Fibonacci series using recursion in java
import java.util.Scanner; public class Fibonacci2 { //using recursion
static int i=0,a=0,b=1,c,n;
static void printfibonacci(int n){
if (i<n){
c=a+b;
a=b;
b=c;
System.out.println(c);
printfibonacci(n-1);
}
}
public static void main(String[] args) {
Scanner na = new Scanner(System.in);
System.out.println("n:- ");
int n = na.nextInt();
System.out.println("Fibonacci Series:- ");
System.out.println(a+"\n"+b);
printfibonacci(n-2);
}
}
Output:-
n:- 10
Fibonacci Series:-
0
1
1
2
3
5
8
13
21
34
This comment has been removed by the author.
ReplyDelete