 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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. 
Advertisements
 