Monday, 24 December 2012

Printing the Pascal Triangle:


Program for the Pascal Triangle:

//Pascal triangle...

#include<stdio.h>
#include<conio.h>
void main()
{
int p[10][10];
int i,j,row,m,n;
clrscr();
printf("Enter the number of rows you want to get printed:");
scanf("%d",&row);
p[0][0]=1;
p[1][0]=1;
p[1][1]=1;
for(i=2;i<row;i++)
{
for(j=0;j<=i;j++)
{
if(j==0)
{
p[i][0]=1;
}
else
{
 if(i==j)
 {
p[i][i]=1;
 }
 else
 {
p[i][j]=p[i-1][j-1]+p[i-1][j];
 }
}
}
}
for(m=0;m<row;m++)
{
for(n=0;n<=m;n++)
{
printf("%d ",p[m][n]);
}
printf("\n");
}
getch();
}

and the output is like:


0 Responses to “Printing the Pascal Triangle:”

Post a Comment

Recent Comments

All Rights Reserved Assignments on C language