Saturday, 1 September 2012

Fibonacci java program


Fibonacci
Output
1:- 1
2:- 1
3:- 2
4:- 3
5:- 5
6:- 8
7:- 13
8:- 21
9:- 34
10:- 55
11:- 89
12:- 144
13:- 233
14:- 377
15:- 610

Source code
package com.swain.cell;

public class Fibonacci {

        public static long fibonacci(int n) {
               if (n <= 1) return n;
               else return fibonacci(n-1) + fibonacci(n-2);
           }

           public static void main(String[] args) {
               int n=15;
               for (int i = 1; i <= n; i++)
                   System.out.println(i + ":- " + fibonacci(i));
           }

}

No comments:

Post a Comment