-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion.cpp
More file actions
58 lines (57 loc) · 874 Bytes
/
Recursion.cpp
File metadata and controls
58 lines (57 loc) · 874 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
using namespace std;
int sum(int n)
{
if (n == 1)
return 1;
return n + sum(n - 1);
}
int power(int n, int p)
{
if (p == 0)
return 1;
return n * power(n, p - 1);
}
int factorial(int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int fib(int n)
{
if (n == 0 || n == 1)
return n;
return fib(n - 1) + fib(n - 2);
}
void dec(int n)
{
if (n == 1)
{
cout << "1" << endl;
return;
}
cout << n << endl;
dec(n - 1);
}
void inc(int n)
{
if (n == 1)
{
cout << "1" << endl;
return;
}
inc(n - 1);
cout << n << endl;
}
int main()
{
int n;
cin >> n;
// cout << sum(n) << endl;
// cout << power(n, 3) << endl;
// cout << factorial(n) << endl;
// cout << fib(n) << endl;
// inc(n);
return 0;
}