C++ program to make pyramid of numbers of Numbers
This is the proper c++ program that makes pyramid of numbers by prompting number of rows of pyramid from user.
Enter numbers of rows:
and pyramid is made.
User side:
1: User asked to enter the numbers of rows
2: User displayed with pyramid.
Developer side
1: Declare variables that are going to be used in program.
2: Input the numbers of rows from user(input in an int variable)
3: Start a for loop from 1 to the numbers of rows entered by user
4: Start an inner loop from 1 to less then rows (entered by user) minus the counter of above loop 'a'.
5: In the inner loop display a space and increase count variable by 1. loop ends here.
6: Inside the external start a while loop until the following condition stands true (c!=2*a-1)
7: Inside the while loop check if count variable is less than equal to rows minus 1.
8: If above condition is true display the addition of the counter of external for loop 'a' and
'c' that is initially zero. Increase 'count' variable by 1.
9: Else (Condition is false) increase the 'count1' variable by 1 and display the following (a+c-2*count1).
10: Increase 'c' variable by 1. Internal for loop ends here .
11: Assign zero to 'count', 'count1' and 'c' variables.
12: Display a next line using 'endl'. External for loop ends here.
13: Program ends here.
Code:
#include <iostream>
using namespace std;
int main()
{
int a,space,r,c=0,count=0,count1=0;
cout
<< "Enter the number of rows: ";
cin
>> r;
for(a=1;a<=r;++a)
{
for(space=1;space<=r-a;++space)
{
cout << " ";
++count;
}
while(c!=2*a-1)
{
if (count<=r-1)
{
cout << " " <<
(a+c);
++count;
}
else
{
++count1;
cout << " " <<
(a+c-2*count1);
}
++c;
}
count1=count=c=0;
cout
<< endl;
}
system("pause");
return 0;
}
Output:
Comments
Post a Comment