Write a program in C to make such a pattern like a pyramid with numbers increased by 1. The pattern is as follows:
Flowchart of C++ program to print pyramid pattern of numbers
C++ program to print pyramid pattern of numbers
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 s,r ,i,j,k; cout<<"Please input number of rows: "; cin>>r; s=(r+4)-1; i=1; do { k=s; do { cout<<" "; k--; } while(k>=1); j=1; do { cout<<"* "; j++; } while(j<=i); cout<<endl; s--; i++; } while(i<=r); } |
Output
Excercise
Find the possible mistakes in the following Shamil’s Flow Table of the C++ program to print pyramid pattern of numbers.
loop
r=3 |
condition | What line will Execute
|
Actual work to do
|
what we are getting as output in each step
|
k=s ,s=6
k=6
|
1,2,3,4,5,6,7,8,9,10,11, 12, 13,
14,15,16,17 |
k—
|
_ | |
k=5 | True | 1,2,3,4,5,6,7,8,9,10,11, 12, 13, 14,
15,16,17,18, 13, 14,15,16,17
|
k—
|
__ |
k=4 | True | 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
|
k—
|
___ |
k=3 | True | 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,13, 14,15,16,17 |
k—
|
____ |
k=2 | True | 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,13, 14,15,16,17,18,13, 14, 15,16,17 |
k—
|
_____ |
k=1 | True | 1,2,3,4,5,6,7,8,9,10,11, 12, 13, 14,15, 16,17,18, 14,15,16,17,18,13, 14,15,16,17 18, 13, 14,15,16,17,18,13, 14, 15,16,17,18, 13, 14,15,16,17 |
k—
|
______ |
k=0
j=1 |
False for k=0
True for j=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,13, 14,15,16,17,18,13, 14,15,16,17,18, 13, 14,15,16, 17,18,19,20,21,22,23,24 |
j++ | ______* |
j=2 | False for j=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,13, 14,15,16,17 18, 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,27,28,29 |
“/n”
s— i++ |
|
i=2
K=s , s=5 |
True | 30,10,11, 12, 13, 14,15,16,17 | K– | ______*
_ |
I=2
K=4 |
True | 30,10,11, 12, 13, 14,15,16,17,18,
13, 14,15,16,17 |
K– | ______* __ |
I=2
K=3 |
True | 30,10,11, 12, 13, 14,15,16,17,
18,13, 14,15,16,17,18,13, 14,15,16,17 |
K– | ______*
__ |
I=2
K=2 |
True | 30,10,11, 12, 13, 14,15,16,17,18,
13, 14,15,16,17,18,13, 14,15,16,17, 18,13, 14,15,16,17 |
K– | ______*
___ |
I=2
K=1 |
True | 30,10,11, 12, 13, 14,15,16,17,18,13,
14,15,16,17,18,13, 14,15,16,17,18, 13, 14,15,16,17,18,13, 14,15,16,17 |
K– | ______*
____ |
I=2
J=1 K=0 |
False for k=0
True for j=1 |
30,10,11, 12, 13, 14,15,16,17,18,13,
14,15,16,17,18,13, 14,15,16,17,18, 13, 14,15,16,17,18,13, 14,15,16, 17,18,19,20,21,22,23, 24 |
J++ | ______*
_____* |
I=2
J=2 |
30,10,11, 12, 13, 14,15,16,17,18,13,
14,15,16,17,18,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,20,21,22,23,24 |
J++ | ______*
_____*_* |
|
I=2
J=3 |
False | 30,10,11, 12, 13, 14,15,16,17,18,13,
14,15,16,17,18,13, 14,15,16,17,18,1 3,14,15,16,17,18,13, 14,15,16,17, 18,19,20,21,22,23, 24,25,20,21,22,23,24,25,26, 27,28,29 |
“/n”
S– i++ |
|
I=3
S=4, K=s, k=4 |
True | 30,10,11, 12, 13, 14,15,16,17 | k–
k=3 |
______*
_____*_* _ |
i=3
k=3 |
True | 30,10,11, 12, 13, 14,15,16,17,
18,13, 14,15,16,17 |
k–
k=2 |
______*
_____*_* __ |
i=3
k=2 |
True | 30,10,11, 12, 13, 14,15,16,17,
18,13, 14,15,16,17,18, 13, 14,15,16,17 |
k–
k=1 |
______*
_____*_* ___ |
i=3
k=1 |
True | 30,10,11, 12, 13, 14,15,16,17,
18,13, 14,15,16,17,18, 13, 14, 15,16,17,18,13, 14,15,16,17 |
k– | ______*
_____*_* ___ |
i=3
k=0 j=1 |
False for k=0
True for j=1 |
30,10,11, 12, 13, 14,15,16,17,
18,13, 14,15,16,17,18, 13, 14, 15,16,17,18,13, 14,15, 16,17,18,19,20,21,22,23, 24 |
j++ | ______*
_____*_* ____* |
i=3
j=2 |
True | 30,10,11, 12, 13, 14,15,16,17,
18,13, 14,15,16,17,18,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,20,21,22,23,24 |
j++ | ______*
_____*_* ____*_* |
i=3
j=3 |
True | 30,10,11, 12, 13, 14,15,16,17,18,
13, 14,15,16,17,18,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,20,21,22,23,24, 25,20, 21,22,23,24 |
j++ | ______*
_____*_* ____*_*_* |
i=3
j=4 |
False | 30,10,11, 12, 13, 14,15,16,17,18,
13, 14,15,16,17,18,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,20,21,22,23,24, 25,20,21, 22,23,24,25,26,27,28,29 |
“/n”
s– i++ |
|
i=4 | False | 30,10,11, 12, 13, 14,15,16,17,18,
13, 14,15,16,17,18,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,20,21,22,23,24, 25,20,21,22,23,24,25,26,27, 28,29,30,31 |
End |