Program to display Pascal triangle in C++?

Follow
  • Follow publicly
  • Follow privately
  • Unfollow
C++ is a computer language
Best Answer
#include<iostream.h>
void main()
{
int a[10][10],i,j,n;
cout<<" Enter the final row of Pascal's triangle:";
cin>>n;
for(int i=0;i<n;i++)
{ a[i][0]=1;}
for(i=1;i<=n;i++)
{ a[i][i+1]=1;}
for(int i=2;i<n;i++)
{
for(j=1;j<=i;j++)
{
a[i][j]=a[i-1][j-1]+a[i-1]a[j];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<i+1;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<endl;
}

I have used a bi-directional array for this. if you want its explaination, i can explain it to you on patnikrishna@yahoo.co.in
  • Rate
  • Comment

Other Answers (1)

  • Pashya answered 7 years ago
    #include <iostream>
    using namespace std;
    int main()
    {
    int n,k,i,x;
    cout << "Enter a row number for Pascal's Triangle: ";
    cin >> n;
    for(i=0;i<=n;i++)
    {
    x=1;
    for(k=0;k<=i;k++)
    {
    cout << x << " ";
    x = x * (i - k) / (k + 1);
    }
    cout << endl;
    }
    return 0;
    }

    Source(s):

    • 2
    • Comment
  • Sign In 

    to add your answer

Who is following this question?

    %
    BEST ANSWERS
    Member since:
    Points: Points: Level
    Total answers:
    Points this week:
    Follow
     
    Unfollow
     
    Block
     
    Unblock