# Thread: nth Fibonacci number using recursion

1. ## nth Fibonacci number using recursion

Hi

I have done the following code to find the Nth Fibonacci number. It works. But now I need to do find the Nth Fibonacci number using recursion. How do I do this?

Link: Recursive Fibonacci Sequence - C++ - Source Code | DreamInCode.net

Code:
```// fibo_recursion_simple.cpp
// finding nth Fibonacci number

#include <iostream>
#include <cstdlib>

using namespace std;

unsigned long fibo(unsigned long n);

int main()
{
int n;

cout << "which Fibonacci number to find, e.g 5th,: ";
cin >> n;

cout << n << "th Fibonacci number is: " << answer << endl;

system("pause");

return 0;
}

//------------------------------------------------------
// fibo() definition

unsigned long fibo(unsigned long n)
{

if (n >= 3)
{
// fibo(n) = fibo(n-1) + fibo(n-2)

const unsigned long limit = 4294967295;
unsigned long next_to_last = 0;
unsigned long last = 1;
int i = 0;

while( next_to_last < limit/2 )
{

long new_last = next_to_last + last;
next_to_last = last;
last = new_last;

i = i++;

if ( i == n )
{
break;
}

}

return (next_to_last);

}

else
{
return 1;
}
}```

2. So now that you've found the answer, instead of trying to coming up with it on your own you, you'd like to put some time into understanding how it works before doing a copy and paste?
I think the code is pretty self-explanatory. If there's any bit you can't work out, just ask questions about it.

3. Hi

Originally Posted by iMalc
So now that you've found the answer, instead of trying to coming up with it on your own you,
Thanks for the reply. In my previous post I included the code for finding the nth Fibonacci number using the while only to show you that I know how to tackle the problem generally. I didn't (still don't) understand how to do it using recursion.

So, you find the below given code sel-explanatory! The code given below is completely wrong. I have compiled it and run it. Please help me.

Code:
```#include <iostream>

using namespace std;

long fibcalc(unsigned long n);

int main()
{

int n;
cout << "Please enter a number for the fibonacci sequence. " << endl;
cin >> n;
fibcalc(n);

}

long fibcalc(unsigned long n)
{

if (n <= 1)
{
cout << n;
return n;
}
else
{
return fibcalc(n-1)+fibcalc(n-2);
}

}```

4. If you don't do
Code:
`cout << fibcalc(n);`
how do you have any idea what value your function is calculating?

5. ## lol

I would probably get kicked out of school for this, but here is what I came up with.

Code:
```#include <iostream>

using namespace std;

long fibonA(long numO, long numT, long findN, int plOne, int plTwo, int plThree) {
long sumInt;
sumInt = numO + numT;
if (plOne == findN) {
return numO;
}
if (plTwo == findN) {
return numT;
}
if (plThree == findN) {
return (numO + numT);
}
plOne += 3;
plTwo += 3;
plThree += 3;

return fibonA((sumInt + numT), ((sumInt + numT) + sumInt), findN, plOne, plTwo, plThree);

}

int main() {
long findNum, numOne = 0, numTwo = 1;
cout << "Which number do you want to find : ";
cin >> findNum;
cout << endl << "The number is : " << fibonA(numOne, numTwo, findNum, 1, 2, 3) << endl;
return 0;
}```
EDIT: For clarity I should add that I count 0 as 1. (sequence A000045 in OEIS) off the Wikipedia page.

Also seems from the algorithm that wikipedia gives you that they want to start at 1,2,3,5,8 ... if that's the case then I just plugged in the algorithm on the site, and it works.
Not sure why you posted though as it took me about a minute to bring a website up with full code ( I was just curious after my laughable code above ). hehe.

Code:
```#include <iostream>

using namespace std;

long fiboN(long varA)
{
if ((varA == 0) || (varA == 1))
return 1;
else
return fiboN(varA - 1) + fiboN(varA - 2);
}

int main()
{

long checkN;
cout << "Enter fibonacci number to check : ";
cin >> checkN;
cout << endl << fiboN(checkN) << endl;
return 0;

}```

6. Originally Posted by jackson6612
The code given below is completely wrong. I have compiled it and run it. Please help me.

Code:
```#include <iostream>

using namespace std;

long fibcalc(unsigned long n);

int main()
{

int n;
cout << "Please enter a number for the fibonacci sequence. " << endl;
cin >> n;
fibcalc(n);

}

long fibcalc(unsigned long n)
{

if (n <= 1)
{
cout << n;
return n;
}
else
{
return fibcalc(n-1)+fibcalc(n-2);
}

}```
It's not completely wrong, the only wrong bits are the cout statement you added inside the recursive function, and a lack of outputting the value that it returned back to the call from main.

If you want to check if a car's emission levels are okay, do you put the sensor inside one of the cylinders of the engine, or do you put it where stuff is emitted from the tailpipe?

7. Thanks a lot, tabstop, xentaka, iMalc. You guys have really helped me. I genuinely appreciate this.

Regards
Jackson