🎓 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
A number pyramid pattern is a triangular arrangement of numbers, where the numbers increase in each row to form a pyramid. This exercise helps in understanding how to use loops to control the alignment and placement of numbers.
Problem Statement
Create a Python program that:
- Accepts the number of rows for the pyramid.
- Prints a pyramid pattern with numbers.
Example:
- Input:
rows = 5 - Output:
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Solution Steps
- Input the Number of Rows: The user specifies how many rows the pyramid should have.
- Use Nested Loops: The outer loop handles the rows, and the inner loops handle printing the numbers and spaces.
- Display the Number Pyramid: Numbers are printed in increasing order, and spaces are used to align the numbers into a pyramid shape.
Python Program
# Step 1: Input the number of rows for the pyramid
rows = int(input("Enter the number of rows: "))
# Step 2: Outer loop for rows
for i in range(1, rows + 1):
# Step 3: Print spaces for alignment
print(" " * (rows - i), end="")
# Step 4: Print numbers in increasing order
for j in range(1, i + 1):
print(j, end=" ")
# Move to the next line after printing each row
print()
Explanation
Step 1: Input the Number of Rows
- The program starts by asking the user for the number of rows, which will define the height of the pyramid.
Step 2: Outer Loop for Rows
- The outer loop controls the number of rows printed, running from
1torows.
Step 3: Print Spaces for Alignment
- The first inner part prints spaces to align the numbers properly, forming the pyramid shape.
Step 4: Print Numbers
- The second inner loop prints numbers in increasing order, starting from
1and going up to the current row number (i).
Output Example
For rows = 5, the output will be:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
For rows = 4, the output will be:
1
1 2
1 2 3
1 2 3 4
Conclusion
This Python program prints a number pyramid pattern using nested loops. The program prints numbers in increasing order and spaces to align the numbers into a pyramid shape. This exercise helps in practicing loop control and formatting output 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