Monday, December 21, 2015

java program to show fibonacci :


java program to show fibonacci :

public class MyFibonacci {
  public static int fibonacciRecusion(int number){
    if(number == 1 || number == 2){
      return 1;
    }
   
    return fibonacciRecusion(number-1) + fibonacciRecusion(number -2);
  }
 
  public static void main(String a[]){  
    int febCount = 50;
    int[] feb = new int[febCount];
    feb[0] = 1;
    feb[1] = 1;
   
    for(int i=2; i < febCount; i++){
      feb[i] = feb[i-1] + feb[i-2];
    }
   
    System.out.println("Fibonacci by loop and array");
    for(int i=0; i< febCount; i++){
      System.out.print(feb[i] + " ");
    }
   
    System.out.println("\nFibonacci by recusion");
    for(int i=1; i<=febCount; i++){
      System.out.print(fibonacciRecusion(i) +" ");
    }
  }
}

0 comments:

Post a Comment