-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern.cpp
More file actions
70 lines (63 loc) · 1.9 KB
/
pattern.cpp
File metadata and controls
70 lines (63 loc) · 1.9 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
#include<iostream>
using namespace std;
// void pattern1(int n) {
// // Step 1: Focus on the number of rows (outer loop): n rows
// for (int i = 0; i <= n; i++) {
// // Step 2: Focus on the number of columns (inner loop): n columns
// for (int j = 0; j<= n-i+1; j++) {
// // Step 3: Printing ' * ' in the inner loop
// cout<< j;
// }
// cout<<endl;
// // Step 4: Observing Symmetry: NOT REQUIRED
// }
// }
// int main() {
// int n;
// cout << "Enter the number of rows/columns for the pattern: ";
// cin >> n;
// pattern1(n);
// return 0;
// }
/*
void pattern7(int n) {
// Step 1: Number of rows: n
for (int i = 0; i < n; i++) {
// Step 2.1: Spaces - Number of columns: n - i - 1, e.g. if n = 4, step 1: 3, step 2: 2, step 3: 1, step 4: 0
for (int j = 0; j < n-i-1; j++) {
// Step 3.1: Printing spaces n-i-1 times
cout<<" ";
}
// Step 2.2: Stars - Number of columns: 2 * i + 1, e.g. step 1: 1, step 2: 3, step 3: 5, step 4: 7
for (int j = 0; j < 2*i+1 ; j++) {
// Step 3.1: Printing stars 2*i+1 times
cout<<"* ";
}
cout<<endl;
// Step 4: Observing Symmetry: NOT REQUIRED
}
}
*/
void patternNew(int n) {
// Step 1: Number of rows: n
for (int i = 0; i < n; i++) {
// Step 2.1: Spaces - Number of columns: n - i - 1
for (int j = 0; j < n-i-1; j++) {
// Step 3.1: Printing spaces n-i-1 times (Single space for alignment)
cout<<" ";
}
// Step 2.2: Stars - Number of columns: i + 1
for (int j = 0; j <= i ; j++) {
// Step 3.1: Printing stars i+1 times
cout<<"* ";
}
cout<<endl;
}
}
int main() {
int n;
cout << "Enter the number of rows for the pattern: ";
cin >> n;
patternNew(n);
return 0;
}