21 March, 2012

C++ Program For Printing Pascal's Triangle.

This Post Contains A C++ Program For Printing Pascal's Triangle With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'If...else', 'For-Loop', 'While-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 For Printing Pascal's Triangle.

/* Declaration Of Header Files */
# include <iostream.h>
# include <conio.h>

/* Start Of Main Program */
void main()
{
              
               /* Declaration Of Variables */
               int b, p, q, r, x;
               clrscr();

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

               /* Source Code For Computing Pascal's Triangle */
               b = 1;
               q = 0;
               cout << "\n Pascal triangle : \n";
               while ( q < r )
               {
                        for ( p = 30 - 3 * q; p > 0; p-- )
                        cout <<" ";
                        for ( x = 0; x <= q; x++ )
                        {
                                   if ( (x == 0) || (q == 0) )
                                  {
                                             b = 1;
                                   }
                                   else
                                   {
                                              b = ( b * (q-x+1) / x );
                                   }
                                    cout <<"    "<<b;
                         }
                         cout <<"\n";
                         q++;
               }
               getch();
}
/* End Of Main Program */ 



Output  :







No comments:

Post a Comment

Subscribe To:

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