🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
Pascal’s Triangle is a triangular array of binomial coefficients. Each entry in Pascal’s Triangle is the sum of the two directly above it. This pattern is an excellent exercise for understanding loops and mathematical operations in Python.
Problem Statement
Create a Python program that:
- Accepts the number of rows for Pascal’s Triangle.
- Prints Pascal’s Triangle in a triangular format.
Example:
- Input:
rows = 5 - Output:
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
Solution Steps
- Input the Number of Rows: The user specifies how many rows of Pascal’s Triangle to generate.
- Use Nested Loops: The outer loop handles the rows, and the inner loop handles printing the elements in Pascal’s Triangle.
- Calculate Pascal's Triangle Values: Each value in Pascal’s Triangle is computed using the binomial coefficient formula
C(n, k) = n! / (k! * (n - k)!). - Display Pascal’s Triangle: Print the values in a triangular format, with spaces for alignment.
Python Program
# Step 1: Define a function to calculate the factorial of a number
def factorial(n):
if n == 0 or n == 1:
return 1
else:
fact = 1
for i in range(2, n + 1):
fact *= i
return fact
# Step 2: Define a function to calculate binomial coefficient C(n, k)
def binomial_coefficient(n, k):
return factorial(n) // (factorial(k) * factorial(n - k))
# Step 3: Input the number of rows for Pascal’s Triangle
rows = int(input("Enter the number of rows: "))
# Step 4: Outer loop to print each row of Pascal's Triangle
for i in range(rows):
# Print leading spaces for alignment
print(" " * (rows - i), end="")
# Print the elements of Pascal's Triangle using binomial coefficient
for j in range(i + 1):
print(binomial_coefficient(i, j), end=" ")
# Move to the next line after each row
print()
Explanation
Step 1: Define the Factorial Function
- The function
factorial()calculates the factorial of a numbern, which is used to compute the binomial coefficients.
Step 2: Define the Binomial Coefficient Function
- The function
binomial_coefficient(n, k)calculates the binomial coefficient using the formulaC(n, k) = n! / (k! * (n - k)!). This is how we compute each element in Pascal’s Triangle.
Step 3: Input Number of Rows
- The program asks the user to input how many rows of Pascal’s Triangle to generate.
Step 4: Print Pascal’s Triangle
- The outer loop controls the number of rows printed.
- Leading spaces are printed to align the triangle.
- The inner loop calculates and prints each element in the row using the
binomial_coefficient()function.
Output Example
For rows = 5, the output will be:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
For rows = 6, the output will be:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Conclusion
This Python program prints Pascal’s Triangle using nested loops and the binomial coefficient formula. The program aligns the output in a triangular format using spaces and prints each element of Pascal’s Triangle using a combination of factorial and binomial coefficient calculations. This exercise helps in practicing loops, functions, and mathematical operations in Python.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment