Python Program to Print the Pascal's triangle for n number of rows given by the user



When it is required to print the pascal’s triangle for a specific number of rows, where the number is entered by the user, a simple ‘for’ loop is used.

Below is the demonstration of the same −

Example

 Live Demo

from math import factorial input = int(input("Enter the number of rows...")) for i in range(input):    for j in range(input-i+1):       print(end=" ")    for j in range(i+1):       print(factorial(i)//(factorial(j)*factorial(i-j)), end=" ") print()

Output

Enter the number of rows...6       1      1 1     1 2 1    1 3 3 1   1 4 6 4 1 1 5 10 10 5 1

Explanation

  • The required packages are imported.

  • The number of rows is taken as input from the user.

  • The number is iterated over in the form of a nested loop.

  • The factorial method is used to print the pascal’s triangle on the console.

Updated on: 2021-04-19T10:52:34+05:30

634 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements