Pascal Triangle
The below program is going to print the Pascal Triangle:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,n,c,k,space;
clrscr();
printf("Enter the limit ");
scanf("%d",&n);
printf("\n\n");
space=n;
for(i=0;i<=n;i++)
{
c=1;
for(k=space;k>=0;k--)
printf(" ");
space--;
for(j=0;j<=i;j++)
{
printf("%d ",c);
c=(c*(i-j)/(j+1));
}
printf("\n");
}
getch();
}
0 row = 1
1 row = adding the two numbers above them to the left and the right
= (0+1) , (1+0)
= 1 , 1
2 row = (0+1) , (1+1) , (1+0)
= 1 , 2 , 1
3 row = (0+1), (1+2), (2+1), (1+0)
= 1 , 3 , 3 , 1
4 row = (0+1), (1+3) , (3+3), (3+1), (1+0)
= 1 , 4 , 6 , 4 , 1
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,n,c,k,space;
clrscr();
printf("Enter the limit ");
scanf("%d",&n);
printf("\n\n");
space=n;
for(i=0;i<=n;i++)
{
c=1;
for(k=space;k>=0;k--)
printf(" ");
space--;
for(j=0;j<=i;j++)
{
printf("%d ",c);
c=(c*(i-j)/(j+1));
}
printf("\n");
}
getch();
}
Comments
Post a Comment