Write a program that reads a positive odd whole number n and prints a pyramid wh
ID: 3673238 • Letter: W
Question
Write a program that reads a positive odd whole number n and prints a pyramid where the first row contains all numbers from 1 to n (i.e., ascending order), the second row displays all numbers from 2 to n – 1, the third row displays all numbers from 3 to n – 2, etc. The last row will contain only a single value, i.e. the middle value in the range, i.e. 1 to n. Each successive row contains two fewer values than the previous row, i.e. the two values at the ends of the previous row are missing. For example, if n is 9, then the program will output:
123456789
2345678
34567
456
5
If n is 13, then the program will output:
1234567890123
23456789012
345678901
4567890
56789
678
7
The i’th row contains n – (2i – 2) values. Each column displays the same number. If a row has more than 10 digits, the digit after 9 should start again from 0.
Explanation / Answer
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,a=1,b,n,s;
cout<<"Enter lines : ";
cin>>n;
for(i=n; i>=1; i--)
{
s=n-i;
while(s!=0)
{
cout<<" ";
s--;
}
for(j=1; j<i*2; j++)
{
if(j<i)
{
cout<<a;
a++;
}
else if(j==i)
{
cout<<a;
b=a;
}
else
{
++b;
cout<<b;
a--;
}
}
a++;
cout<<" ";
}
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.