Program in C++ to make such a pattern like a pyramid with a number that will repeat the number in the same row.
Write a program in C++ to make such a pattern like a pyramid with a number that will repeat the number in the same row.
Flowchart to make such a pattern like a pyramid
C++ Program (Source Code) to make such a pattern like a pyramid
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 | #include<iostream> using namespace std; int main() { int r,sp,disp,n,no; cout<<"Please enter the number of rows"; cin>>no; n=no; r=1; do { sp=1; do { cout<<" "; sp++; } while(sp<=n); n--; disp=1; do { cout<<" "<<r; disp++ ; } while(disp<=r); cout<<endl; r++; } while(r<=no); } |
Output
Excercise
Find the possible mistakes in the following Shamil’s Flow Table of the Program to make such a pattern like a pyramid with a number that will repeat the number in the same row.
What statement will be executed? |
Loop(r)=1, Loop(sp)=1 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 output? – |
Loop(r)=1, Loop(sp)=2 1,2,3,4,5,6,7,8,9,10,11,12,13,14 ,15,16,17,18, 13,14,15,16,17,18
output? — |
Loop(r)=1, Loop(sp)=3 1,2,3,4,5,6,7,8,9,10,11,12,13,14 ,15,16,17,18,13,14,15,16,17,18, 13,14,15,16,17,18 output? —
|
Loop(r)=1, Loop(sp)=4, Loop(disp)=1 1,2,3,4,5,6,7,8,9,10,11,12,13,14 ,15,16,17,18, 13,14,15,16,17,18, 13,14,15,16,17,18 19,20,21,22,23,24,25,26 output? —1
|
Loop(r)=1, Loop(sp)=4, Loop(disp)=2 1,2,3,4,5,6,7,8,9,10,11,12,(13,14,15,16,17,18,)^3,19,20,21,22,23,24,25,26,27,28,29,30 output? Move to Next line
|
Loop(r)=2, Loop(sp)=1 1,2,3,4,5,6,7,8,9,10,11,12,(13,14,15,16,17,18)^3,19,20,21,22,23,24,25,26,27,28,29,30,10,11,12,13,14,15,16,17,18 output? –
|
Loop(r)=2, Loop(sp)=2, 1,2,3,4,5,6,7,8,9,10,11,12,(13,14,15,16,17,18)^3,19,20,21,22,23,24,25,26,27,28,29,30,10,11,12,13,14,15,16,17,18,10,11,12,13,14,15,16,17,18 output? —
|
Loop(r)=2, Loop(sp)=3, Loop(disp)=1 1,2,3,4,5,6,7,8,9,10,11,12,(13,14,15,16,17,18)^3,19,20,21,22,23,24,25, 26,27,28,29,30,(10,11,12,13,14,15,16,17,18)^2,19,20,21,22,23,24,25,26 output? –2
|
Loop(r)=2, Loop(sp)=3, Loop(disp)=2 1,2,3,4,5,6,7,8,9,10,11,12,(13,14,15,16,17,18)^3,19,20,21,22,23,24,25,26,27,28,29,30,(10,11,12,13,14,15,16,17,18)^2,19,20,21,22 ,23,24,25,26,21,22,23,24,25,26 output? –2-2 |
Loop(r)=2, Loop(sp)=3, Loop(disp)=3 1,2,3,4,5,6,7,8,9,10,11,12,(13,14,15,16,17,18)^3,19,20,21,22,23,24,25,26,27,28,29,30,(10,11,12,13,14,15,16,17,18)^2,19,20,(21,22,23,24,25,26)^2,27,28,29, output? Move to Next line |
Loop(r)=3, Loop(sp)=1 1,2,3,4,5,6,7,8,9,10,11,12,(13,14,15,16,17,18)^3,19,20,21,22,23,24,25,26,27,28,29,30,(10,11,12,13,14,15,16,17,18)^2,19,20,(21,22,23,24,25,26)^2,27,28,29,30,10,11,12,13,14,15,16,17,18 output? –
|
Loop(r)=3, Loop(sp)=2, Loop(disp)=1 1,2,3,4,5,6,7,8,9,10,11,12,(13,14,15,16,17,18)^3,19,20,21,22,23,24,25,26,27,28,29,30(10,11,12,13,14,15,16,17,18)^2,19,20,(21,22,23,24,25,26)^2,27,28,29,30,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26 output? -3
|
Loop(r)=3, Loop(sp)=2, Loop(disp)=2 1,2,3,4,5,6,7,8,9,10,11,12,(13,14,15,16,17,18)^3,19,20,21,22,23,24,25,26,27,28,29,30(10,11,12,13,14,15,16,17,18)^2,19,20,(21,22,23,24,25,26)^2,27,28,29,30,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,21,22,23,24,25,26 output? -3-3
|
Loop(r)=3, Loop(sp)=2, Loop(disp)=3 1,2,3,4,5,6,7,8,9,10,11,12,(13,14,15,16,17,18)^3,19,20,21,22,23,24,25,26,27,28,29,30,(10,11,12,13,14,15,16,17,18)^2,19,20,(21,22,23,24,25,26)^2,27,28,29,30,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,21,22,23,24,25,26,21,22,23,24,25,26 output? -3-3-3
|
Loop(r)=3, Loop(sp)=2, Loop(disp)=4 1,2,3,4,5,6,7,8,9,10,11,12,(13,14,15,16,17,18)^3,19,20,21,22,23,24,25,26,27,28,29,30.(10,11,12,13,14,15,16,17,18)^2,19,20,(21,22,23,24,25,26)^2,27,28,29,30,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,21,22,23,24,25,26,21,22,23,24,25,26,27,28,29,30,31
|