Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Fibonnaci Recursion without for or while loop

967983Apr 8 2013 — edited Apr 19 2013
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,
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 17 2013
Added on Apr 8 2013
22 comments
557 views