Hello,

I wrote a code to calculate the fibonacci number. However, i noticed that it starts acting erratic when i input 1000 i.e it cannot calculate the 1000th fibonacci number.

I would appreciate it if you could show me how to make it work.

int f(int &n)
         {
           if ((n == 2) || (n == 1))
	     return(1);
           else
	     return(f(n-1) + f(n-2));
         }

thank you in advance for your help

Are you sure you want

int f (int & n)

I get an error when I compile the above but I don't when I do this:

int f (int n)

Does anybody else get an error?

oh! u r right. sorry about that. i was messing up with the code to see if i could get the 1000th number but i guess i somehow put that in there.
thanks

Are you sure you want

int f (int  n)

I get an error when I compile the above but I don't when I do this:

int f (int n)

Does anybody else get an error?

The simplest fib sequence you can write is pretty much

int fib( int n ){return ( n<2 )?n:fib( n-1 )+fib( n-2 );}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.