forked from phani653/Pattern-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern-without-loop.cpp
52 lines (45 loc) · 1023 Bytes
/
pattern-without-loop.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
// C++ program to print pattern that first reduces 5 one
// by one, then adds 5. Without any loop
#include <iostream>
using namespace std;
// Recursive function to print the pattern.
// n indicates input value
// m indicates current value to be printed
// flag indicates whether we need to add 5 or
// subtract 5. Initially flag is true.
void printPattern(int n, int m, bool flag)
{
// Print m.
cout << m << " ";
// If we are moving back toward the n and
// we have reached there, then we are done
if (flag == false && n ==m)
return;
// If we are moving toward 0 or negative.
if (flag)
{
// If m is greater, then 5, recur with true flag
if (m-5 > 0)
printPattern(n, m-5, true);
else // recur with false flag
printPattern(n, m-5, false);
}
else // If flag is false.
printPattern(n, m+5, false);
}
void PrintPattern(int m)
{
if (m > 0)
{
cout << m << '\n';
PrintPattern(m - 5);
}
cout << m << '\n';
}
int main()
{
int n = 16;
//printPattern(n, n, true);
PrintPattern(n);
return 0;
}