🎓 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 hollow diamond pattern is formed by two symmetric triangular patterns—one upright and the other inverted—with stars (*) printed along the boundary and spaces inside, giving it a hollow effect. This exercise helps in understanding how to control loops and use conditional logic to print stars and spaces.
Problem Statement
Create a Python program that:
- Accepts the number of rows (half the height of the diamond).
- Prints a hollow diamond-shaped pattern using stars (
*).
Example:
- Input:
rows = 5 - Output:
* * * * * * * * * * * * * * * *
Solution Steps
- Input the Number of Rows: The user specifies the number of rows, which determines half the height of the diamond.
- Use Nested Loops: The outer loops control the rows, and the inner loops handle printing the stars and spaces for both the upper and lower parts of the diamond.
- Conditionally Print Stars and Spaces: Stars are printed on the boundary of the diamond, and spaces are printed inside to create the hollow effect.
Python Program
# Step 1: Input the number of rows for half the diamond
rows = int(input("Enter the number of rows: "))
# Step 2: Print the upper part of the diamond
for i in range(1, rows + 1):
# Print leading spaces for alignment
print(" " * (rows - i), end="")
# Print stars and spaces for the hollow pattern
for j in range(1, 2 * i):
if j == 1 or j == 2 * i - 1:
print("*", end="")
else:
print(" ", end="")
print()
# Step 3: Print the lower part of the diamond
for i in range(rows - 1, 0, -1):
# Print leading spaces for alignment
print(" " * (rows - i), end="")
# Print stars and spaces for the hollow pattern
for j in range(1, 2 * i):
if j == 1 or j == 2 * i - 1:
print("*", end="")
else:
print(" ", end="")
print()
Explanation
Step 1: Input the Number of Rows
- The program starts by asking the user for the number of rows, which defines half the height of the diamond (the upper part).
Step 2: Print the Upper Part of the Diamond
- The outer loop handles the number of rows for the upper triangle of the diamond.
- The first part prints spaces to align the stars to form a triangular shape.
- The second part conditionally prints stars (
*) at the boundaries (first and last position of each row), while spaces are printed inside to create the hollow effect.
Step 3: Print the Lower Part of the Diamond
- The outer loop controls the rows for the lower triangle of the diamond, which is inverted.
- The same logic is applied for spaces and stars, but this time the loop decreases to form the inverted triangle.
Output Example
For rows = 5, the output will be:
*
* *
* *
* *
* *
* *
* *
* *
*
For rows = 4, the output will be:
*
* *
* *
* *
* *
* *
*
Conclusion
This Python program prints a hollow diamond pattern using stars (*). The program uses nested loops to print the upper and lower parts of the diamond, with conditional logic to print stars at the boundary and spaces inside to create the hollow effect. 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