Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program pyramid.cpp that reads a positive odd whole number n and prints

ID: 3925544 • Letter: W

Question

Write a program pyramid.cpp 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.

DO NOT DO THE REVERSE OF THIS. THE NUMBERS MUST LOOK EXACTLY LIKE ABOVE

Explanation / Answer

#include <iostream>

using namespace std;

int main()
{
int n;
cout << "Enter odd number n" << endl;
cin>>n;
if(n%2==0)
{
cout<<"You have entered an even number.Please enter odd number.";
}
else
{
int i,j,a=1,b,lines,space;
cout<<"Enter lines : ";
cin>>lines;

       for(i=lines; i>=1; i--)
       {
           space=lines-i;
           while(space!=0)
           {
               cout<<" ";
               space--;
           }
           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();
   }
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote