Fibonacci with recursion
parent
ec8bdb7c84
commit
b95504a57e
|
@ -184,6 +184,7 @@
|
||||||
|
|
||||||
- [Tower of Hanoi](Recursion/towerofHanoi.cpp)
|
- [Tower of Hanoi](Recursion/towerofHanoi.cpp)
|
||||||
- [Factorial](Recursion/factorial.cpp)
|
- [Factorial](Recursion/factorial.cpp)
|
||||||
|
- [Fibonacci](Recursion/fibonacci.cpp)
|
||||||
- [Permutation](Recursion/permutation.cpp)
|
- [Permutation](Recursion/permutation.cpp)
|
||||||
- [GCD/HCF of two numbers](Recursion/GCD-of-two-numbers.cpp)
|
- [GCD/HCF of two numbers](Recursion/GCD-of-two-numbers.cpp)
|
||||||
- [Sum of all elements of an array](Recursion/Sum-of-all-elements-in-an-array.cpp)
|
- [Sum of all elements of an array](Recursion/Sum-of-all-elements-in-an-array.cpp)
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
/*
|
||||||
|
Description: Program to calculate fibonacci using recursion
|
||||||
|
*/
|
||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int RecursiveFibonacci(int number)
|
||||||
|
{
|
||||||
|
if(number == 1 || number == 2)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
if(number >= 3)
|
||||||
|
return RecursiveFibonacci(number - 1) + RecursiveFibonacci(number - 2);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int number = 10;
|
||||||
|
int result = RecursiveFibonacci(number);
|
||||||
|
cout <<"Fibonacci of " <<number <<": " << result << "\n";
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue