-
Notifications
You must be signed in to change notification settings - Fork 7
/
recursion.cpp
41 lines (36 loc) · 885 Bytes
/
recursion.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
using namespace std;
int fib(int n){
if(n<2){
return 1;
}
return fib(n-2) + fib(n-1);
}
// fib(5)
// fib(4) + fib(3)
// fib(2) + fib(3) + fib(2) + fib(3)
int factorial(int n){
if (n<=1){
return 1;
}
return n * factorial(n-1);
}
// Step by step calculation of factorial(4)
// factorial(4) = 4 * factorial(3);
// factorial(4) = 4 * 3 * factorial(2);
// factorial(4) = 4 * 3 * 2 * factorial(1);
// factorial(4) = 4 * 3 * 2 * 1;
// factorial(4) = 24;
int main(){
// Factorial of a number:
// 6! = 6*5*4*3*2*1 = 720
// 0! = 1 by definition
// 1! = 1 by definition
// n! = n * (n-1)!
int a;
cout<<"Enter a number"<<endl;
cin>>a;
// cout<<"The factorial of "<<a<< " is "<<factorial(a)<<endl;
cout<<"The term in fibonacci sequence at position "<<a<< " is "<<fib(a)<<endl;
return 0;
}