/* Program to print pascal's triangle for n powers */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,k,n,r,P,C,rows;
int factorial,temp,result;
cout<<" Enter the no. of rows : ";
cin>>rows;
for (n=0;n<=rows;n++) //logic for rows
{
for (k=rows;k>=n+1;k--) //logic for blank spaces
cout<<" ";
{
for (i=1,factorial=1;i<=n;i++) //calc n!
factorial=factorial*i;
for (r=0;r<=n;r++)
{
for (i=1,temp=1;i<=r;i++) //calc r!
temp=temp*i;
for (i=1,result=1;i<=(n-r);i++) //calc (n-r)!
result=result*i;
P=factorial/result;
C=P/temp;
cout<<" "<<C;
}
}
cout<<endl;
}
getch();
}
/*
OUTPUT :
Enter the no. of rows : 6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
*/
Monday, October 3, 2011
Pascals Triangle
Labels:
C++ Programs
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment