Monday, 7 December 2020

Fibonacci Series using Recursion

import java.util.*;
public class FibRecursion {
	int fibSeries(int n){
		if(n<=1) 
		{
			return n; 
		}
		return fibSeries(n-1)+fibSeries(n-2);
	}
	public static void main(String[] args) 
	{
		Scanner scan = new Scanner(System.in);
		System.out.println("\n Enter Limit...");
		int N = scan.nextInt();
		FibRecursion f = new FibRecursion();
		f.fibSeries(N);
		for(int i=0; i<N; i++)
		{
			System.out.print(" " + f.fibSeries(i));
		}		
	}
}

Output:

Enter Limit...
10
 0 1 1 2 3 5 8 13 21 34



No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32