Pascal’s Triangle Algorithm and Flowchart

3
13836

Along with the source code, the algorithm and flowchart for Pascal’s triangle is a common problem in any high level language, especially C and C++. In this post, I have presented a simple algorithm and flowchart for Pascal’s triangle along with a brief introduction to Pascal’s triangle, it’s generation mechanism and some of its important properties. Both the algorithm and flowchart are generate Pascal’s triangle in standard format as per the number of rows entered by the user.

Pascal’s Triangle is a system of numbers arranged in rows resembling a triangle with each row consisting of the coefficients in the expansion of (a + b)n for n = 0, 1, 2, 3. The construction of the triangular array in Pascal’s triangle is related to the binomial coefficients by Pascal’s rule. Before going through the Pascal’s triangle algorithm and flowchart, here’s a look at it’s properties, and more importantly how the triangle is generated.

How to Generate Pascal’s Triangle?

Pascal’s Triangle Algorithm and Flowchart

Most of us who are familiar with Pascal’s triangle don’t know that the numbers outside the triangle are all “0”‘s. And, these “0”s are very important for the triangular pattern to work so as to form a triangular array. The triangle starts with a number “1” at the top, and each new number added below the top “1″ is just the sum of the two numbers above, except for the edge which are all “1″s. Here’s how the rows are formed:

0 row =1
1 row = (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

Properties of Pascal’s Triangle:

Pascal’s Triangle Algorithm and Flowchart

  1. In Pascal’s triangle, the sum of all the numbers of a row is twice the sum of all the numbers of the previous row. So, the sum of 2nd row is 1+1= 2, and that of 1st is 1. Again, the sum of 3rd row  is 1+2+1 =4, and that of 2nd row is 1+1 =2, and so on. This major property is utilized here in Pascal’s triangle algorithm and flowchart.
  2. The edges, left and right, of the triangle consist of “1”s only.
  3. And, all the elements outside the triangle are “0”s.
  4. The sequence of the product of each element or number is related to e, the base of the natural logarithm.
  5. The diagonal next to the edge diagonal, in both left and right, contain natural numbers (1, 2, 3, ….) in order.
  6. The sum of the squares of the numbers of row “n” equals the middle number of row “2n”.

Pascal’s Triangle Algorithm:

  1. Start
  2. Declare variables x, y, n, a, z, s
  3. Enter the limit
  4. Initialize the value of variables, s=n , x=0, y=0 , z=s
  5. Do the following operations in loop
    1. x = 0 to n
    2. a= 1, x++
    3. z=s to 0
    4. print space
    5. z—-
    6. y = o to x
    7. print a
    8. a = a*(x-y)/(y+1)
    9. y= y+1
    10. go to next line
  6. Repeat the process to n
  7. Print the final required triangle
  8. Stop

Pascal’s Triangle Flowchart:

Pascal’s Triangle FlowchartAlso see,
Pascal’s Triangle C Program

The algorithm and flowchart for Pascal’s triangle discussed here can be used to write source code for Pascal’s triangle in any high level programming language. If you have any queries regarding this algorithm or flowchart, mention and discuss them in the comments section below.

3 COMMENTS

  1. The following is a BASIC implementation (tested using the BaCon BASIC to C converter) of the Pascal’s triangle algorithm:

    REM pascal.bac
    REM BaCon program to generate Pascal’s triangle
    REM Change n for different number of rows

    REM Variables are x, y, n, a, z, s
    REM n = number of rows to generate
    n = 5
    REM Initialize variables
    s = n: x = 0: y = 0: z = s
    REM Print initial “1”
    PRINT 1
    REM Outer loop
    WHILE x <= n
    a = 1: x = x + 1
    IF z < 0 THEN
    PRINT " ";
    END IF
    z = z – 1
    REM Inner loop
    FOR y = 0 TO x
    PRINT a; :PRINT " ";
    a = a * (x – y)/(y + 1)
    REM y = y + 1 (performed by NEXT)
    REM End of inner loop
    NEXT
    PRINT
    REM End of outer loop
    WEND
    END

    I added code to print the initial row containing "1". As coded, the rows are displayed flush left on the screen. On the flowchart, in the computation box just below the "print space" box, it should be z– instead of s–.

LEAVE A REPLY

Please enter your comment!
Please enter your name here