C Program Pascal Triangle Pattern Explanation

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.

c-program-pascal-triangular-pattern-concept-digitalpadm
pascal-triangular-pattern-concept

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)

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-conceptThe remaining elements are calculated by formula ans=(ans*(row-cols+1)/(cols)

Download Code :  c-program-pascal-triangle-code.c (24 downloads)
OUTPUT

Nested looping statements and pyramid, ascending,descending triangular patterns