-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtut18.cpp
More file actions
36 lines (30 loc) · 716 Bytes
/
Copy pathtut18.cpp
File metadata and controls
36 lines (30 loc) · 716 Bytes
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
#include<iostream>
using namespace std;
int fib(int n){
if(n <= 1){
return 1;
}
return fib(n-2) + fib(n-1);
}
int factorial(int n){
if (n <= 1){
return 1;
}
return n * factorial(n-1);
}
// Step by step calcualtion of factorial
// 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:
// n! = n * (n-1)!
int a;
cout<<"Enter a number: "<<endl;
cin>>a;
cout<<"The factorial of "<<a<<" is "<<factorial(a)<<endl;
cout<<"The "<<a<<"th term in Fibonacci sequence is "<<fib(a)<<endl;
return 0;
}