20 February, 2013

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 < stdio.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 */
                printf ( " \n Enter No. Of Rows : " );
                scanf ( " %d " , &r );

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

Output  :





No comments:

Post a Comment

Subscribe To:

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