21 March, 2012

C++ Program To Print Pyramid Of Numbers.

This Post Contains A C++ Program To Print Pyramid Of Numbers With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'For-loop' & 'Nested Loops' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C++ Program To Print Pyramid Of Numbers.

/* Declaration Of Header Files */

# include <iostream.h>
# include <conio.h>

/* Start Of Main Program */
void main()
{
              
                /* Declaration Of Variables */
                int   i,   j,   k,   t,   r;
                clrscr();

                /* Asking For The Input From User */
                cout << " Enter No. Of Rows : ";
                cin >> r;

                /* Source Code For Computing Pyramid Of Numbers */
                t  =  1;
                for (  i  = 1;  i  <=  r;  i++  )
                {
                           for ( j=0; j < ( r - i ); j++ )
                          {
                                        cout <<" ";
                          }
                          k = ( i * 2 ) - 1;
                          for (  j  =  0;  j  <  k;  j++  )
                         {
                                        if (  t  >  99  )
                                                cout <<t;
                                        else
                                                if (  t  <  10  )
                                                          cout <<  "  "  <<  t;
                                                else
                                                          cout <<  "  " <<  t;
                                                           t++;
                         }
                         cout <<  "\n\n";
                }
                getch();
}
/* End Of Main Program */ 

Output  :





1 comment:

  1. creating pyramid of numbers is the basic lab c program used in many institutions. Thank you for sharing this program.
    regards:
    srinath reddy.
    admin of Programming Tutorials for Beginners

    ReplyDelete

Subscribe To:

Most Commonly Asked Programs In 'C' & 'C++' Language.