Write a program for Fibonacci Series with recursion concept.
import java.util.Scanner; public class FibonacciSeries { public static void main(String[] args) { int x=0,y=1; Scanner sc=new Scanner(System.in); System.out.println("Enter a Number:-"); int z=sc.nextInt(); System.out.println("The fibo series is:- "); fiboSeries(z,x,y); } private static void fiboSeries(int x,int f,int f1) { if(x==0) System.exit(0); System.out.print(" " + (f+f1)); fiboSeries(--x,f1,f+f1); } } Output: Enter a Number:- 8 The fibo series is:- 1 2 3 5 8 13 21 34 Enter a Number:- 10 The fibo series is:- 1 2 3 5 8 13 21 34 55 89
No comments:
Post a Comment