Java program to print fibonacci series of n numbers.

import java.util.*;

public class Fib{

public static void main(String args[]){

int a,b,c,n;

Scanner s=new Scanner(System.in);

System.out.println("How many terms do you want in fibonacci series\n");

n=s.nextInt();

if(n>0){

System.out.println("Enter initial two numbers\n");

a=s.nextInt();

b=s.nextInt();

System.out.println("Fibonacci series of"+n+ "term is:\n");

if(n==1){

System.out.println(a);

System.exit(0);}

if(n==2){

System.out.println(a+" "+b);

System.exit(0);}

System.out.print(a+" "+b+" ");

int i;

for(i=1;i<=n-2;i++){

c=a+b;

System.out.print(c+" ");

a=b;

b=c;

}

}

else{

System.out.println("Wrong number of term");

}

}

}

Comments

Popular posts from this blog

What is a Web Browser? वेब ब्राउज़र क्या है?

Java's Support System जावा का सहयोगी तंत्र

The Internet and Java इंटरनेट और जावा का सम्बन्ध