-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path03_increasing_decreasing_recursion.cpp
More file actions
67 lines (54 loc) · 1.18 KB
/
03_increasing_decreasing_recursion.cpp
File metadata and controls
67 lines (54 loc) · 1.18 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
/*
Topic - Increasing Decreasing Recursion
Write 2 Functions to Print first N numbers in
- Increasing
- Descreasing
Easiest Way to Approach to Recursive Problems
"Magical" Recursive Rule = Principle of Mathematical Induction (PMI)
1. Figure out the Base Case
2. Assume Sub Problem can be solved by recursion (automatically)
3. Using the sub-problem write the answer for the current problem.
*/
#include <iostream>
using namespace std;
void dec(int n)
{
// base case
if(n==0)
{
return;
}
// recursive case
cout << n << " "; // Before function call, goes from Top to Base Case
dec(n-1);
}
void inc(int n)
{
// base case
if(n==0)
{
return;
}
// recursive case
inc(n-1);
cout << n << " "; // After function call, goes in Bottom up direction (i.e from Base case towards Top)
}
// function to drive code
int main()
{
int n;
cout << "Enter Number: ";
cin >> n;
cout << "Decreasing: ";
dec(n);
cout << "\nIncreasing: ";
inc(n);
cout << endl;
return 0;
}
/*
OUTPUT:
Enter Number: 5
Decreasing: 5 4 3 2 1
Increasing: 1 2 3 4 5
*/