-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTriangleOfNumbers.cpp
More file actions
61 lines (57 loc) · 918 Bytes
/
TriangleOfNumbers.cpp
File metadata and controls
61 lines (57 loc) · 918 Bytes
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
/*Print the following pattern for the given number of rows.
Pattern for N = 4
1
232
34543
4567654
*/
#include<iostream>
using namespace std;
int main(){
//First Approach
int totalrows = 4;
int row = 1;
while(row <= totalrows){
int col = 1;
int num = row;
while(col <= totalrows + row -1){
if(col <= (totalrows - row)){
cout<<" ";
}else if(col < totalrows){
cout<<num;
num = num+1;
}else{
cout<<num;
num = num-1;
}
col++;
}
row++;
cout<<"\n";
}
cout<<"----Second Approach----";
//Second Approach
int totalrows_2 = 4;
int row_2 = 1;
while(row_2 <= totalrows_2){
int spaces = 1;
while(spaces < totalrows_2 - row_2){
cout<<" ";
spaces++;
}
int temp = row_2;
int col_2 = 1;
while(col_2 <= row_2){
cout<<temp;
temp++;
}
temp--;
int k = 1;
while(k < row_2){
temp--;
cout<<temp;
}
row_2++;
cout<<"\n";
}
}