Skip to content

Added Fibonacci using recursion in c++ #632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Recursion/Fibonacci.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>
using namespace std;
//recursive fibonacci function to find our nth digit of fibonacci series
int fibonacci(int x) {
if((x==1)||(x==0)) {
return(x);//if nth term is 0 or 1 it will return 0 or 1
}else {
return(fibonacci(x-1)+fibonacci(x-2));//nth term is sum of its previous two terms
}
}
int main() {
int x;
cout << "Enter nth digit: ";
cin >> x;
cout << " " << fibonacci(x-1);//x-1 because 0 is our 1st term of fibonacci series

return 0;
}