-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursion.cpp
More file actions
50 lines (39 loc) · 781 Bytes
/
recursion.cpp
File metadata and controls
50 lines (39 loc) · 781 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
#include <iostream>
using namespace std;
int factorial(int n){
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int sum_of_natural_numbers(int n){
if (n == 1)
return 1;
return n + sum_of_natural_numbers(n - 1);
}
void ascending(int n){
if (n == 0)
return;
ascending(n - 1);
cout << n << " ";
}
void decending(int n){
if (n == 0)
return;
cout << n << " ";
decending(n - 1);
}
int fibonacci(int n){
if (n == 0 || n == 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main(){
int n;
cin >> n;
cout << factorial(n) << endl;
cout << sum_of_natural_numbers(n) << endl;
ascending(n);
decending(n);
cout << fibonacci(n) << endl;
return 0;
}