Write a program pyramidcpp that reads a positive odd whole n

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

Solution

#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;
}

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site