Q. Write a C program to generate pascal triangle.

**OR**

Q. Write a C program to print following number triangle :

1 | ||||||||

1 | 1 | |||||||

1 | 2 | 1 | ||||||

1 | 3 | 3 | 1 | |||||

1 | 4 | 6 | 4 | 1 |

**Ans.**

**How to build Pascal triangle:**

### To build the pascal triangle, start with "1" at the top, then continue placing numbers below it in a triangular pattern. Each number is build just sum of above two number, (except for the edge, which are all ‘1’ and all numbers outside the Triangle are 0's). so

### 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

/*c program for making pascal triangle*/

**#include**<stdio.h>

**#include**<conio.h>

long calc( int );

int main()

**{**

int i,j,row,pas;

printf("Enter no. of rows in pascal triangle : ");

scanf("%d",

**&**row);
for(i=0; i<row; i++)

{

for(j=0; j<=(row-i-1); j++)

printf(" ");

for(j=0; j<=i; j++)

{

pas=calc(i)/(calc(j)*calc(i-j));

printf("%ld ",pas); //take single space

}

printf("\n");

}

getch();

getch();

**return 0**;

**}**

long calc( int num)

{

int x;

long res=1;

for(x=1; x<=num; x++)

res=res*x;

**return**(res);

}

__Output__:-
simple way with out using function....

ReplyDelete#include

#include

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();

}

good its so simple, thanks

DeleteExcellent ... and great effort... U proved u r a great programmer :)

Deleteit works good but if u enter the no of rows as 2,in the output three rows are printed i.e. if u enter the no rows as a,the output is printed with a+1 rows

Delete@Aravind Swamy,

DeleteI checked above program again and find that,

the output will be equal of the entered no of rows.

Try some different compiler.

thank u dinilps

Deletethis program is working

friend i want to algorith

Deletei dint undestand what u ment by initializing i and j kindly assist am a new lleaner

DeleteFor those who are getting (n+1)th row as output

DeleteTry this code...

#include

void main()

{

int i,j,n,c,k,space,q;

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(" ");

q=i-1;

space--;

for(j=0;j<=q;j++)

{

printf("%d ",c);

c=(c*(q-j)/(j+1));

}

printf("\n");

}

not working for limit grater then 5. this is not universal, using upto 4. and also there is bug of space, there is 3 rows are blank and after that printing started

Deletethis is not working more than 5 rows. and also 3 or 4 rows are blank and then printing starts

Deletei think the comment is the best method to solve it.the given is quite logical bt the next is ultimate.

ReplyDeletethe above prog is good bt u hv to declare the pass as long else it give u garbage value...

ReplyDeleteSimpler Approach!

ReplyDelete#include

void main()

{

int i,j,x,n,s;

printf("Enter The Number : ");

scanf("%d",&n);

for(i=0;i<=n;i++)

{

x=1;

for(s=1;s<=n-i;s++)

printf("\t");

for(j=1;j<=i+1;j++)

{

printf("%d\t\t",x);

x=x*(i-j+1)/j;

}

printf("\n");

}

}

every thing is correct except that in:

Deleteline 11 : printf(" ");

line 14 : printf("%d ",x); //single space

change these things in the program and it will run

Right jai raj

Deleteits not printing the required output

ReplyDelete@artwel junior,

DeleteWrite down your code, for solving error.

Try some other differ compiler.

#include

ReplyDelete#include

void main()

{

int i,j,sp,num=1,r;

clrscr();

for(i=1;i<=5;i++)

{

r=num;

for(sp=i;sp<=5;sp++)

printf(" "); //2 space

for(j=1;j<=i;j++)

{

printf("%d ",r%10); //3 space

r=r/10;

}

num=num*11;

printf("\n");

}

getch();

}

not working in quincy ...

Deletecan i have a flow chart and algorithm for this?

ReplyDeleteThis comment has been removed by the author.

ReplyDeleteThis comment has been removed by a blog administrator.

ReplyDeletecan any one explain this logic for me

ReplyDeletegood

ReplyDeletesuperb....!

ReplyDelete#include

ReplyDelete#include

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();

}

#include

ReplyDelete#include

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();

}

#include

ReplyDelete#include

long calc( int );

int main()

{

int i,j,row,pas;

printf("Enter no. of rows in pascal triangle : ");

scanf("%d", &row);

for(i=0; i<row; i++)

{

for(j=0; j<=(row-i-1); j++)

printf(" ");

for(j=0; j<=i; j++)

{

pas=calc(i)/(calc(j)*calc(i-j));

printf("%ld ",pas); //take single space

}

printf("\n");

}

getch()

#include

ReplyDelete#include

#include

int fact(int a)

{

int b,fact=1;

for(b=1;b<=a;b++)

fact=fact*b;

return fact;

}

main()

{

int a,b,c,row,ele;

printf("Enter number of row you want to print:");

scanf("%d",&row);

for(a=0;a<=row;a++)

{

for(b=0;b<=(row-a+2);b++)

{

printf(" ");

}

for(c=0;c<=a;c++)

{

ele=fact(a)/(fact(c)*fact(a-c));

printf("%d ",ele);

}

printf("\n");

}

}

functiopn is best method for pascal triangle as it uses a concept of bionomial coeff

Deleteam trying to run this above in my machine and is not working what is the mistake am i doing ??

Deletecan this work ??

#include

#include

int main()

{

int i,j,n,c,k,space;

printf("Enter the number to limit up on");

scanf("%d",&n);

printf("\n\n");

for(i=0;i<=n;i++)

{

c=1;

for(k=n;k>=0;k--)

printf(" ");

n--;

for(j=0;j<=i;j++)

{

printf("%d ",c);

c=(c*(i-j)/(j+1));

}

printf("\n");

}

return 0;

}

54381

ReplyDelete4381

381

81

1

aisa parogram kise ko ata ha yaha

#include

Delete#include

void main()

{

int i,j,a[5]={5,4,3,8,1};

for(i=5;i>0;i--)

{

for(j=1;j<5;j++)

{

printf("%d",&a[j]);

}

printf("\n");

}

getch();

}

void main()

ReplyDelete{

int i,j,n,c,k;

printf("Enter the limit ");

scanf("%d",&n);

printf("\n\n");

for(i=0;i<=n;i++)

{

c=1;

for(k=0; k<=n-i; k++)

{

printf(" ");

}

for(j=0; j<=i; j++)

{

printf("%d ",c);

c=(c*(i-j)/(j+1));

}

printf("\n");

}

getch();

}

here error is not present but op is blank screen

ReplyDelete