Write a program of Fibonacci series using Recursion in Java

Code:
 import java.util.Scanner;  
 public class FibonacciRecursion {  
      public static void main(String args[]) {  
           Scanner scanner = new Scanner(System.in);  
           System.out.println("Enter the number for Fibonacci series:");  
           int num = scanner.nextInt();  
           System.out.println("Fibonacci Series:");  
           for (int i=0;i<num;i++) {  
           System.out.print(fibonacci(i) + ",");  
           }  
           scanner.close();  
      }  
      public static int fibonacci(int num) {  
           if (num == 0) {  
                return 0;  
           } else if (num == 1) {  
                return 1;  
           } else {  
                return fibonacci(num-2) + fibonacci(num-1);  
           }  
      }  
 }  
Output :
Enter the number for Fibonacci series:
8
Fibonacci series:
0,1,1,2,3,5,8,13,

No comments: