Fibonacci Series or Fibonacci numbers are the number in the following integer sequence
0,1,1,2,3,5,8,13,21,34,55,89,144.......
The first two numbers in Fibonacci series are 0 and 1 and each subsequent number is sum of previous two.
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by
Fn=Fn-1+Fn-2, where F0=0 and F1=1
Input:- Generate Fibonacci series upto Limit 10
Ouptut:- 0 1 1 2 3 4 8 13 21 34
Sample Program:-
/**
* @author Dixit
*
*/
public class FibonacciExample {
/**
* @param args
*/
public static void main(String[] args) {
// number of elements to generate in a series
int limit = 10;
long[] fibonacciSeries = new long[limit];
// create first 2 series elements
fibonacciSeries[0] = 0;
fibonacciSeries[1] = 1;
// create the Fibonacci series and store it in an array
for (int i = 2; i < limit; i++) {
fibonacciSeries[i] = fibonacciSeries[i - 1]
+ fibonacciSeries[i - 2];
}
// print the Fibonacci series numbers
System.out.println("Fibonacci Series upto the limit :" + limit);
for (int i = 0; i < limit; i++) {
System.out.print(fibonacciSeries[i] + " ");
}
}
}
Output
Fibonacci Series upto the limit :10
0 1 1 2 3 5 8 13 21 34
Enjoy Programming
No comments:
Post a Comment