Pascal triangle

kameshwar 0 Tallied Votes 142 Views Share

Pascal's triangle is a geometric arrangement of the binomial coefficients in a triangle. The rows of Pascal's triangle are conventionally enumerated starting with row zero, and the numbers in odd rows are usually staggered relative to the numbers in even rows. A simple construction of the triangle proceeds in the following manner. On the zeroth row, write only the number 1. Then, to construct the elements of following rows, add the number directly above and to the left with the number directly above and to the right to find the new value. If either the number to the right or left is not present, substitute a zero in its place.

#include  <iostream>

using namespace std;

struct x{
	int a[100];
};

struct x b[100];

int main(){
    int n;
	cout<<"Enter value of N: ";
	cin>>n;
	for(int i=0;i<n;i++){
		b[i].a[0]=1;
		for(int j=1;j<=i;j++){
			if(i==1){
				b[i].a[0]=1;
				b[i].a[1]=1;
			}
			else
				b[i].a[j]=b[i-1].a[j-1]+b[i-1].a[j];
		}
		b[i].a[i]=1;
	}

	int m=1;
	int x=n;

	for(i=0;i<x;i++){
		for(int j=0;j<n-1;j++)
			cout<<" ";
		for(int k=0;k<m;k++){
			cout<<b[i].a[k];
			cout<<" ";
		}
		cout<<endl;
		n--;
		m++;
	}
	return EXIT_SUCCESS;
}