pascal triangle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,z,k;float h=1;
printf("enter the level upto which u wish to print the pascal triangle");
scanf("%d",&z);--z;
for(i=0;i<=z;i++)
{

 for(j=1;j<=z-i;j++)
 {printf(" ");} // to prrovide space to obtain triangular shape
for(j=0;j<=i;j++)
{
 for(k=0;k<=j-1;k++)
{
h*=(i-k)/(j-k);
} //to print the binomial coefficients....
k=h;
printf("%d ",k);
h=1;
}printf("\n");//to begin from another line of the program..
}
getch();
}

My concern is that the triple loop in the program makes it complex.....is there a simpler alternative......please suggest.......
the output shud be like

   1
 1 2 1 
1 3 3 1 
Last edited on
plz sumone answer........
This is a C++ forum. Your code is C.
Yes you can do it in with 2 for statements. I had to do this for homework, it sucked.
Actually i'm not sure because mine isn't printed out as a triangle.
Last edited on
@novablows cud u share wat u did?
I had a for loop for the rows, and a for loop for the columns. I had it so that the loop for run like this: row 1 col 1, row 2 col1 col 2, row 3 col 1 col 2 col 3, row 4 col 1 col 2 col 3 col 4...etc. For each new row I had the value reset back to 1. Now all you have to do is find the pattern that will solve each individual number.
Topic archived. No new replies allowed.