program for printing the fibonacci series upto n using recursion in java
Code::
import java.util.Scanner;
public class Test{
static int a=0,b=1,c;
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.println("Enter the number ");
int n = scn.nextInt();
if(n==1){
System.out.println(0);
}else if(n==2){
System.out.println(0+" "+1);
}else{
System.out.print(0+" "+1+" ");
fib(n-2);
}
}
public static void fib(int n){
if(n==0){
System.out.println();
return;
}
c = a+b;
a = b;
b = c;
System.out.print(c+" ");
fib(n-1);
}
}
Comments
Post a Comment