Python Program to Print Right Arrow Star Pattern

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

  1. Input the Number of Rows: The user specifies the number of rows, which defines the height of the right arrow.
  2. Use Nested Loops: The outer loops handle the rows, and the inner loops handle printing the stars.
  3. 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 1 to rows.

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 - 1 to 1.

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.

Comments