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?
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:
- 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.
- The edges, left and right, of the triangle consist of “1”s only.
- And, all the elements outside the triangle are “0”s.
- The sequence of the product of each element or number is related to e, the base of the natural logarithm.
- The diagonal next to the edge diagonal, in both left and right, contain natural numbers (1, 2, 3, ….) in order.
- The sum of the squares of the numbers of row “n” equals the middle number of row “2n”.
Pascal’s Triangle Algorithm:
- Declare variables x, y, n, a, z, s
- Enter the limit
- Initialize the value of variables, s=n , x=0, y=0 , z=s
- Do the following operations in loop
- x = 0 to n
- a= 1, x++
- z=s to 0
- print space
- y = o to x
- print a
- a = a*(x-y)/(y+1)
- y= y+1
- go to next line
- Repeat the process to n
- Print the final required triangle
Pascal’s Triangle Flowchart:
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.