-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #488 from geeky01adarsh/master
Added two recursion programs
- Loading branch information
Showing
3 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
Program's_Contributed_By_Contributors/Recursion/factorial_n.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Find the factorial of n | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int factorial(int n) | ||
{ | ||
if (n == 0) | ||
return 1; | ||
int ans = factorial(n - 1); | ||
return n * ans; | ||
} | ||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
int n; | ||
cin >> n; | ||
cout << factorial(n) << endl; | ||
return 0; | ||
} |
20 changes: 20 additions & 0 deletions
20
Program's_Contributed_By_Contributors/Recursion/n_fabonacci_series.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// find the nth term of fibonacci series using recursion | ||
#include<iostream> | ||
using namespace std; | ||
|
||
int fibonacci(int n){ | ||
if(n==0) | ||
return 0; | ||
else if(n==1) | ||
return 1; | ||
int ans = fibonacci(n-1)+fibonacci(n-2); | ||
return ans; | ||
} | ||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
int n; | ||
cin>>n; | ||
cout<<fibonacci(n)<<endl; | ||
return 0; | ||
} |