Monday, 11 June 2018

Fibonacci series using recursion in java


Sample Program:-


 public class SampleProgram {  
      public static void main(String[] args) {  
           int n = 5;  
           System.out.print("Fibonacci Series: 0 1 ");  
           for (int i = 2; i <= n; i++) {  
                System.out.print(fibonacciRecursion(i) + " ");  
           }  
      }  
      private static int fibonacciRecursion(int n) {  
           if (n == 1 || n == 2) {  
                return 1;  
           }  
           return fibonacciRecursion(n - 1) + fibonacciRecursion(n - 2);  
      }  
 }  

Output:-


 Fibonacci Series: 0 1 1 2 3 5   

Enjoy Coding.

No comments:

Post a Comment