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
i 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
its 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();
}
can i have a flow chart and algorithm for this?
ReplyDelete