Description
This program prints the pascal triangle. Below is an example for pascal triangle. In the first row we only write 1, all the other rows start and end with a 1, the in between numbers are calculated by adding the numbers in the previous row. Eg - in the third row 2 is obtained by adding the two 1's in the previous row. In the fourth row the first 3 is obtained by adding 1 and 2, and the second 3 is obtained by adding 2 and 1 and so on.
Example
111
121
1331
14641
Program
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
{
int a[50][50]={0},i,j,n;
printf("Enter the value for n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
if(i==j|j==0)
{
a[i][j]=1;
}
if(i>2)
{
if(j<i)
{
a[i][j]=a[i-1][j-1]+a[i-1][j];
}}}}
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<i;j++)
{
printf("%d",a[i][j]);
}}
getch();
}
No comments:
Post a Comment