In this program ,you will learn how to print Fibonacci series in java language. The Fibonacci series is a series where next digit is sum of previous digits.


Source Code:

  1. public class FibonocciNumber {
  2. public static void main(String[] args) {
  3. try (Scanner input = new Scanner (System.in)) {
  4. int n,i,first = 0,second = 1,fibo;
  5. System.out.println("How many number: ");
  6. n = input.nextInt();
  7. System.out.println(first+"\n"+second);
  8. for(i=3;i<=n;i++) {
  9. fibo = first + second;
  10. first = second;
  11. second = fibo;
  12. System.out.println(fibo);
  13. }
  14. }
  15.   }
  16. }

output:

How many number: 3
0
1
1

How many number: 12
0
1
1
2
3
5
8
13
21
34
55
89



0 comments:

Post a Comment