🎓 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 right arrow star pattern is a shape where stars (*) form a right-pointing arrow. The number of stars first increases to form the top part of the arrow and then decreases to form the bottom part, creating a symmetrical pattern. This is a good exercise for practicing nested loops and conditional printing in Python.
Problem Statement
Create a Python program that:
- Accepts the number of rows for the right arrow pattern.
- Prints a right arrow-shaped star pattern.
Example:
- Input:
rows = 5 - Output:
* ** *** **** ***** **** *** ** *
Solution Steps
- Input the Number of Rows: The user specifies the number of rows, which defines the height of the right arrow.
- Use Nested Loops: The outer loops handle the rows, and the inner loops handle printing the stars.
- Display the Right Arrow Pattern: First, stars are printed in increasing order for the upper part of the arrow, then in decreasing order for the lower part.
Python Program
# Step 1: Input the number of rows for the right arrow pattern
rows = int(input("Enter the number of rows: "))
# Step 2: Print the upper part of the right arrow
for i in range(1, rows + 1):
print("*" * i)
# Step 3: Print the lower part of the right arrow
for i in range(rows - 1, 0, -1):
print("*" * i)
Explanation
Step 1: Input the Number of Rows
- The program begins by asking the user for the number of rows, which defines the height of the arrow.
Step 2: Print the Upper Part of the Right Arrow
- The first loop prints the stars for the upper part of the arrow, where the number of stars increases from
1torows.
Step 3: Print the Lower Part of the Right Arrow
- The second loop prints the stars for the lower part of the arrow, where the number of stars decreases from
rows - 1to1.
Output Example
For rows = 5, the output will be:
*
**
***
****
*****
****
***
**
*
For rows = 4, the output will be:
*
**
***
****
***
**
*
Conclusion
This Python program prints a right arrow star pattern using nested loops. The stars are printed in increasing order for the upper part of the arrow and decreasing order for the lower part, creating the right arrow shape. This exercise is useful for practicing loop control and pattern printing 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