Video courses for company/skill based Preparation
Purchase mock tests for company/skill building
Python Program to print Pascal’s Triangle
Pascal’s triangle is a triangular array of Binomial Coefficients. It is named after the French Mathematician Blaise Pascal.
Each number in the row is the sum of the left and the right number on the above row. If a number is missing in the row then it is assumed to be 0.The first row starts with 1.
- Number of rows is taken as input from user
- Call function pascalTriangle(n)
- Traverse i from 0 to row and do Step 2 to Step 10(Outer Loop for each row)
- Initialize a coefficient variable c with value 1
- Traverse j from 1 to row-i and do Step 4 (1st Inner loop to print space in a particular row)
- print blank space ” “.
- End of 1st Inner Loop to print blank space
- Traverse k from 0 to i+1 and do Step 7 to Step 8 (2nd Inner loop to print number elements in a particular row)
- print space separated elements in each row
- Calculate c=c*(i-k)//(k+1)
- End of 2nd Inner Loop to print number elements
- Print new line character after each inner iteration
- End of Outer Loop
Output : Enter the number of rows: 7 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1