🎓 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 triangle star pattern consists of stars (*) forming the boundary of a right-angled triangle, while the inside of the triangle is left hollow (filled with spaces). This pattern is a great way to practice using loops and conditional logic in Python.
Problem Statement
Create a Python program that:
- Accepts the number of rows for the triangle.
- Prints a hollow right-angled triangle star pattern.
Example:
- Input:
rows = 5 - Output:
* ** * * * * *****
Solution Steps
- Input the Number of Rows: The user specifies how many rows the triangle should have.
- Use Nested Loops: The outer loop controls the rows, and the inner loop handles printing stars and spaces.
- Conditionally Print Stars: Stars are printed along the boundary (first row, last row, and the first and last columns of each row), while spaces are printed inside to create the hollow effect.
Python Program
# Step 1: Input the number of rows for the hollow triangle
rows = int(input("Enter the number of rows: "))
# Step 2: Outer loop for rows
for i in range(1, rows + 1):
# Step 3: Inner loop for columns
for j in range(1, i + 1):
# Step 4: Print stars at the boundary
if i == rows or j == 1 or j == i:
print("*", end="")
else:
print(" ", end="")
# Move to the next line after each row
print()
Explanation
Step 1: Input the Number of Rows
- The program begins by asking the user for the number of rows for the hollow triangle.
Step 2: Outer Loop for Rows
- The outer loop controls how many rows are printed. It runs from
1torows.
Step 3: Inner Loop for Columns
- The inner loop controls the number of columns printed for each row. It runs from
1toi(whereiis the current row number).
Step 4: Conditional Printing
- Stars (
*) are printed at the boundary:- On the first column (
j == 1), - On the last column of each row (
j == i), - On the last row (
i == rows).
- On the first column (
- Spaces are printed inside the triangle to create the hollow effect.
Output Example
For rows = 5, the output will be:
*
**
* *
* *
*****
For rows = 6, the output will be:
*
**
* *
* *
* *
******
Conclusion
This Python program prints a hollow right-angled triangle star pattern using nested loops and conditional logic. The stars are printed along the boundary of the triangle, while spaces are printed inside to create the hollow effect. This exercise helps in practicing loop control and conditional statements 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