Code:
import java.util.Scanner;public class Fib
{
public static void main(String args[])
{
int n;
Scanner s=new Scanner(System.in);
System.out.println("Enter number upto which Fibonacci series should be printed : ");
n=s.nextInt();
System.out.println("Fibonacci series upto " + n+ " is ");
for(int i=1; i<=n; i++)
{
System.out.print(recursion(i) +" ");
}
}
static int recursion(int num)
{
if(num == 1 || num == 2)
{
return 1;
}
return recursion(num-1) + recursion(num -2);
}
}
No comments:
Post a Comment