Skip to content

Commit f8cbcbb

Browse files
add recursion programs
1 parent c51a3f9 commit f8cbcbb

9 files changed

+194
-0
lines changed

Recurison/Programs/exponent.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int power(int m, int n)
5+
{
6+
if (n == 0)
7+
return 1;
8+
return power(m, n - 1) * m;
9+
}
10+
11+
int main()
12+
{
13+
cout << power(5, 2);
14+
return 0;
15+
}
16+
17+
// output
18+
// 25

Recurison/Programs/factorial.cpp

Whitespace-only changes.
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int F[10];
5+
6+
int fib(int n)
7+
{
8+
if (n <= 1)
9+
{
10+
F[n] = n;
11+
return n;
12+
}
13+
else
14+
{
15+
if (F[n] == -1)
16+
F[n - 2] = fib(n - 2);
17+
if (F[n - 1] == -1)
18+
F[n - 1] = fib(n - 1);
19+
return fib(n - 2) + F[n - 1];
20+
}
21+
}
22+
int main()
23+
{
24+
cout << fib(5);
25+
return 0;
26+
}

Recurison/Programs/fibb.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <iostream>
2+
using namespace std;
3+
int fib(int n)
4+
{
5+
if (n <= 1)
6+
return n;
7+
return fib(n - 2) + fib(n - 1);
8+
}
9+
int main()
10+
{
11+
cout << fib(5);
12+
return 0;
13+
}
14+
15+
// analysis
16+
// for n = 5, 15 calls
17+
// O(2^n)

Recurison/Programs/first_n_sum.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <iostream>
2+
using namespace std;
3+
int sum_n(int n)
4+
{
5+
if (n == 1)
6+
return 1;
7+
return sum_n(n - 1) + n;
8+
}
9+
int main()
10+
{
11+
int n = 5;
12+
cout << sum_n(n);
13+
}

Recurison/Programs/ncr.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <iostream>
2+
using namespace std;
3+
int fact(int n)
4+
{
5+
if (n == 0)
6+
return 1;
7+
return n * fact(n - 1);
8+
}
9+
int ncr(int n, int r)
10+
{
11+
int term_1, term_2, term_3;
12+
term_1 = fact(n);
13+
term_2 = fact(r);
14+
term_3 = fact(n - r);
15+
return term_1 / (term_2 * term_3);
16+
}
17+
int main()
18+
{
19+
cout << ncr(5, 3);
20+
return 0;
21+
}
22+
23+
// Time O(n)
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* combination using Pascal's Triangle
2+
1
3+
/ \
4+
1 1
5+
/ \ / \
6+
1 2 1
7+
/ \ / \ / \
8+
1 3 3 1
9+
10+
Can be represented as:
11+
12+
0c0
13+
/ \
14+
1c0 1c0
15+
/ \ / \
16+
2c0 2c1 2c2
17+
/ \ / \ / \
18+
3c0 3c1 3c2 3c3
19+
20+
*/
21+
22+
#include <iostream>
23+
using namespace std;
24+
int fact(int n)
25+
{
26+
if (n == 0)
27+
return 1;
28+
return n * fact(n - 1);
29+
}
30+
31+
int ncr(int n, int r)
32+
{
33+
if (n == r || r == 0)
34+
return 1;
35+
return ncr(n - 1, r - 1) + ncr(n - 1, r);
36+
}
37+
38+
int main()
39+
{
40+
cout << ncr(10, 2);
41+
return 0;
42+
}

Recurison/Programs/staticVariable.c

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <stdio.h>
2+
3+
int fun1(int n)
4+
{
5+
if (n > 0)
6+
{
7+
return fun1(n - 1) + n;
8+
}
9+
return 0;
10+
}
11+
static int x = 0;
12+
int fun2(int n)
13+
{
14+
if (n > 0)
15+
{
16+
x++;
17+
return fun2(n - 1) + x;
18+
}
19+
return 0;
20+
}
21+
int main()
22+
{
23+
int result1, result2;
24+
result1 = fun1(5);
25+
result2 = fun2(5);
26+
printf("%d\n", result1);
27+
printf("%d", result2);
28+
}
29+
30+
// fun 2 = output 15 sum of first n natural no.s;
31+
// fun 1 = output 25;

Recurison/Programs/tower_of_hanoi.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
// TOH(no. of disks, tower 1, tower 2, tower3)
5+
6+
void TOH(int n, int A, int B, int C)
7+
{
8+
if (n > 0)
9+
{
10+
TOH(n - 1, A, C, B);
11+
static int i = 0;
12+
cout << "Step " << ++i << " (" << A << "," << C << ")" << endl;
13+
TOH(n - 1, B, A, C);
14+
}
15+
}
16+
17+
int main()
18+
{
19+
TOH(64, 1, 2, 3);
20+
return 0;
21+
}
22+
23+
// Analysis
24+
// Time O(2^n)

0 commit comments

Comments
 (0)