Write a C++ program to display Pascal's triangle

#include <iostream>
using namespace std;
void main()
{
int no_row,c=1,blk,i,j;
    cout<<"Input number of rows: ";
    cin>>no_row;
    for(i=0;i<no_row;i++)
    {
        for(blk=1;blk<=no_row-i;blk++)
        cout<<"  ";
        for(j=0;j<=i;j++)
        {
            if (j==0||i==0)
                c=1;
            else
               c=c*(i-j+1)/j;
            cout<<"  1  "<<c;
        }
        cout<<endl;
    }


      system ("pause");
}

Test Data : 
Input number of rows: 5 
Expected Output :

        1
      1   1 
    1   2   1 
  1   3   3   1
1   4   6   4   1
Write a C++ program to display Pascal's triangle Write a C++ program to display Pascal's triangle Reviewed by Unknown on May 30, 2017 Rating: 5

No comments:

Powered by Blogger.