A Pascal’s triangular pattern is array of numbers where the ends of the rows are 1 and each of the others is the sum of the nearest two numbers in the row above. You can find and download code for the working of Nested looping statements and pyramid, ascending, descending triangular patterns.

In pascal triangle, sum of elements of a row is twice the sum of all the elements of its preceding row.

Here, sum of second row is 1+1= 2, and that of first is 1.

The sum of third row is 1+2+1 =4, and that of second row is 1+1 which is 2,

The sum of forth row is 1+3+3+1 =8, and that of third row is 1+2+1 which is 2,

The left and the right edges of Pascal’s triangle are filled with “1”s only. for printing the other elements the formula used to generate the number is:` `

*ans=(ans*(row-cols+1)/(cols)*

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 26 27 28 29 | #include<stdio.h> int main() { int num, rows, cols,spaces,ans; printf("Enter number of rows "); scanf("%d", &num); for(rows=0; rows<num; rows++) //LOOP-1 { for(spaces=1;spaces<=num-rows;spaces++) //LOOP-2 TO INSERT SPACES printf(" "); //printf("**"); for(cols=0;cols<=rows; cols++) //LOOP-3 TO COLUMNS ITERATION { if(cols==0 || rows==0) ans=1; else ans= ans*(rows-cols+1)/cols; printf("%4d",ans); } printf("\n"); } return 0; } |

In this code, Loop-1 is for rows iterations, it iterate for total number of rows entered by user.

Loop-2 is for spaces insertion , if total number of rows as num value is 5, then

- for 1st row we have to insert
`num-rows`

which is (5-0) = 5 double spaces - for 2nd row we have to insert
`num-rows`

which is (5-1) = 4 double spaces - for 3rd row we have to insert
`num-rows`

which is (5-2) = 3 double spaces - for 4th row we have to insert
`num-rows`

which is (5-3) = 2 double spaces - for 5th row we have to insert
`num-rows`

which is (5-4) = 1 double spaces

Loop-3 is for columns printing in each row, The left and the right edges of Pascal’s triangle are filled with “1”s only and other elements are addition of nearest two numbers of its previous row as shown in figure pascal-triangular-pattern-concept. The remaining elements are calculated by formula *ans=(ans*(row-cols+1)/(cols)*

###### Download Code : c-program-pascal-triangle-code.c (43 downloads)

###### OUTPUT

Nested looping statements and pyramid, ascending,descending triangular patterns