-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_72.cpp
More file actions
130 lines (104 loc) · 1.83 KB
/
Day_72.cpp
File metadata and controls
130 lines (104 loc) · 1.83 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include<iostream>
using namespace std;
void fun0(int n)
{
cout<<"Happy Birthday!!\n";
}
void fun1(int n)
{
cout<<n<<" Days left for Birthday\n";
fun0(n-1);
}
void fun2(int n)
{
cout<<n<<" Days left for Birthday\n";
fun1(n-1);
}
void fun3(int n)
{
cout<<n<<" Days left for Birthday\n";
fun2(n-1);
}
void singlefun(int n)
{
if(n == 0)
{
// base case - without this program runs infinitely - segmentation fault occurs in output
cout<<"Happy Birthday!!\n";
return;
}
cout<<n<<" Days left for Birthday\n";
singlefun(n-1); //function calling itself again and again
}
void mfun(int n)
{
// base case
if(n==1)
{
cout<<1<<endl;
return;
}
cout<<n<<endl;
mfun(n-1);
}
void evenfun(int n)
{
// base case
if(n==2)
{
cout<<2<<endl;
return;
}
cout<<n<<endl;
evenfun(n-2);
}
int main()
{
// *** DAy 72 - Introduction To Recursion ***
int n = 3;
/*
// // Iterative approch
// for(int i=n; i>0; i--)
// {
// cout<<i<<" Days left for Birthday\n";
// }
// cout<<"Happy Birthday !!"<<endl;
*/
/*
// Using Functions / Recursion
fun3(3);
//only calling one function
// fun2(2);
// fun1(1);
// fun0(0);
*/
/*
// if value of n is large so we have to write large functions
// to avoid this we can print required o/p using recursion
// function calling itself again and again untill a specific condition
// we can print o/p using only single function - one function
singlefun(5);
*/
// Problem - print m to 1
int m=6;
/*
// -iterative approch
for(int i=m; i>0; i--)
{
cout<<i<<endl;
}
*/
// -Recursive approch
// mfun(m);
// Problem - print k to 2 - all even no. k is even
int k=8;
/*
// -iterative approch
for(int i=k; i>0; i=i-2)
{
cout<<i<<endl;
}
*/
// -Recursive approch
evenfun(k);
}