Hi,
I am trying to print the values of Fibonnaci by using recursion but without using for or while loop.
Any help is appreciated. I have tried different combinations but I am not getting the desired output.The base program I have created is below:
public class Samp {
static int[] fibValue=new int[2];
int a;
int flag0=0;
int flag1=0;
public static void main(String[] args)
{
Samp s = new Samp();
fibValue[0]=0;
fibValue[1]=1;
System.out.print("0,1,");
s.fib(10);
}
int fib(int n)
{
if(n==0)
{
flag0=1;
return 0;
}
if(n==1)
{
flag1=1;
return 1;
}
a=(fib(n-1) + fib(n-2));
System.out.print(a+",");
return a;
}
}
Sample Output:
0,1,1,2,3,5,8,13,21,34,