Skip to content

Commit 07e50ed

Browse files
authored
Added Recursion in C++ Folder
1 parent ba3b693 commit 07e50ed

11 files changed

+128
-0
lines changed

C++/Recursion/recursion1.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include<stdio.h>
2+
3+
void func(int n)
4+
{
5+
if(n>0)
6+
{
7+
func(n-1);
8+
printf("%d ",n);
9+
}
10+
}
11+
12+
int sum(int n)
13+
{
14+
if(n>0)
15+
return sum(n-1)+n;
16+
else
17+
return 0;
18+
19+
}
20+
int main()
21+
{
22+
// int x=3;
23+
// func(x);
24+
printf("%d",sum(5));
25+
26+
return 0;
27+
}

C++/Recursion/recursion1.exe

40.3 KB
Binary file not shown.

C++/Recursion/recursion2.exe

39.8 KB
Binary file not shown.

C++/Recursion/recursion2fib.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include<stdio.h>
2+
void fib(int n)
3+
{
4+
int s1=1,s2=0;
5+
for(int i=1;i<=n;i++)
6+
{
7+
printf("%d ",s1);
8+
s1=s1+s2;
9+
s2=s1-s2;
10+
}
11+
}
12+
13+
int s1=1,s2=0;
14+
int fibr(int n) // Tail recursion.
15+
{ if(n>0)
16+
{
17+
printf("%d ",s1);
18+
s1=s1+s2;
19+
s2=s1-s2;
20+
fibr(n-1);
21+
22+
}
23+
}
24+
int main()
25+
{ fib(10);
26+
return 0;
27+
}

C++/Recursion/recursion2fib.exe

39.8 KB
Binary file not shown.

C++/Recursion/recursion3 (2).exe

40.4 KB
Binary file not shown.

C++/Recursion/recursion3.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include<stdio.h>
2+
int s1=1,s2=0;
3+
int fund(int n) // Tail recursion.
4+
{ if(n>0)
5+
{
6+
printf("%d ",s1);
7+
s1=s1+s2;
8+
s2=s1-s2;
9+
fund(n-1);
10+
11+
}
12+
}
13+
14+
int funa(int n) //Head recursion.
15+
{ if(n>0)
16+
{
17+
funa(n-1);
18+
printf("%d ",s1);
19+
s1=s1+s2;
20+
s2=s1-s2;
21+
22+
}
23+
}
24+
25+
int main()
26+
{
27+
funa(5);
28+
fund(5);
29+
return 0;
30+
}

C++/Recursion/recursion3.exe

40.4 KB
Binary file not shown.

C++/Recursion/recursion3factPow.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include<stdio.h>
2+
int f=1;
3+
int fact(int n)
4+
{ if(n>0)
5+
{ f=f*n;
6+
fact(n-1);
7+
}
8+
return f;
9+
}
10+
int factr(int n)
11+
{ if(n>0)
12+
{
13+
return fact(n-1)*n;
14+
}
15+
else
16+
return 1;
17+
}
18+
19+
int Pow (int num, int n)
20+
{static int res=1;
21+
if(n>0)
22+
{
23+
res=res*num;
24+
Pow(num,n-1);
25+
}
26+
return res;
27+
}
28+
29+
int Powr (int num, int n)
30+
{
31+
if(n>0)
32+
{
33+
return Pow(num,n-1)*num;
34+
}
35+
else
36+
return 1;
37+
}
38+
int main()
39+
{ //int x=Powr(5,3);
40+
//printf("%d",fact(4));
41+
printf("%d",Powr(5,3));
42+
return 0;
43+
}

C++/Recursion/recursion3factPow.exe

40.9 KB
Binary file not shown.

C++/Recursion/tempCodeRunnerFile.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
return Pow(num,n-1)*num;

0 commit comments

Comments
 (0)