-
Notifications
You must be signed in to change notification settings - Fork 0
/
18.cpp
64 lines (51 loc) · 867 Bytes
/
18.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// check whether an array is sorted or not using recursion
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;
// sum of n natural no using recursion
#include <iostream>
using namespace std;
int power(int n,int p){
if(p==0){
return 1;
}
int prevPower = n * power(n,p-1);
// kyunki power till n will be n*(n^p-1)
return prevPower;
}
int main()
{
int n,p;
cin>>n>>p;
cout<<power(n,p);
return 0;
}
// sum of n natural no using recursion
#include <iostream>
using namespace std;
int fact(int n){
if(n==1){
return 1;
}
int Prevfact = n * fact(n-1);
return Prevfact;
}
int main()
{
int n;
cin>>n;
cout<<fact(n);
return 0;
}