Fibonacci series a popular number series and very popular programming question in Java.
In which number is equal to sum of previous two numbers, starting from third.



Program Code :




import java.util.Scanner;

public class FibonacciSeries{

public static void main(String[] args){

Scanner scn=new Scanner(System.in);
int i,n;
int[] fib=new int[50];
System.out.println("Enter how many number do you want [Highest=47]= ");
n=scn.nextInt();
fib[0] = 0;
fib[1] = 1;
for (i = 2; i < n; i++)
fib[i] = fib[i - 1] + fib[i - 2];
System.out.println("The fibonacci series is as follows");
for (i = 0; i < n; i++) 
System.out.println(fib[i]);
}
}


Output :

 Enter how many number do you want [Highest=47]=
10
The fibonacci series is as follows
0
1
1
2
3
5
8
13
21
34

 
Top