-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17.cpp
More file actions
53 lines (40 loc) · 1.09 KB
/
Copy path17.cpp
File metadata and controls
53 lines (40 loc) · 1.09 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>
using namespace std;
// void func(){
// func();
// }
// int main() {
// func();
// // recursion internally works using stack,
// // and since this func() is going on recursively
// // this will lead to stack overflow and thus we get error
// // segmentation fault (error related to memory)
// return 0;
// }
// void func(int n){
// if(n==0) return;
// cout<<n<<endl;
// func(n-1); // thus before function call - operations are done jate hui direction mein
// // and after function call - operations are done wapas aatein hue
// cout<<n<<endl;
// }
// int main(){
// func(5);
// return 0;
// }
// Factorial of N
// every recursive function should have a base condition to return from
int fact(int n){
if(n==0) return 1;
return fact(n-1) * n;
}
int main(){
int n;
cin>>n;
cout<<fact(n)<<"\n";
}
// Time complexity of recursive functions
// 1. Number of function calls
// 2. what is complexity of each function
// O(N * 1) = O(N)
// if for loop is inside the function of fact then O(N * N) = O(N^2)