Fibonacci Series Of a Number Using Java
A program to print Fibonacci series of a given number
The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...The next number is found by adding up the two numbers before it. Below example shows how to create Fibonacci series.
Program
import java.util.Scanner;
public class FibonacciJavaProgram{
public static void main(String a[]){
System.out.print("Enter number up to which Fibonacci series to print: ");
int febCount = new Scanner(System.in).nextInt();
int[] feb = new int[febCount];
feb[0] = 0;
feb[1] = 1;
for(int i=2; i < febCount; i++){
feb[i] = feb[i-1] + feb[i-2];
}
for(int i=0; i< febCount; i++){
System.out.print(feb[i] + " ");
}
}
}
Output
Enter number up to which Fibonacci series to print: 10
0 1 1 2 3 5 8 13 21 34
No comments :
Post a Comment