Introduction
A right-angled triangle star pattern consists of stars (*
) arranged in a triangular shape. This pattern is commonly used to practice loops in programming, where the number of stars increases in each row.
Problem Statement
Create a Python program that:
- Accepts the number of rows for the triangle.
- Prints a right-angled triangle star pattern.
Example:
- Input:
rows = 5
- Output:
* ** *** **** *****
Solution Steps
- Input the Number of Rows: The user defines how many rows the triangle should have.
- Use a Loop: The outer loop controls the rows, and an inner part handles printing the stars.
- Display the Right-Angled Triangle: Print stars in increasing order for each row.
Python Program
# Step 1: Input the number of rows
rows = int(input("Enter the number of rows: "))
# Step 2: Outer loop for rows
for i in range(1, rows + 1):
# Step 3: Print stars for the current row
print("*" * i)
Explanation
Step 1: Input the Number of Rows
- The program starts by asking the user to input the number of rows for the right-angled triangle.
Step 2: Outer Loop for Rows
- The outer loop runs from
1
torows
. It controls how many rows are printed.
Step 3: Print Stars
- The loop prints stars (
*
) in increasing order. In each iteration, the number of stars printed corresponds to the current row numberi
.
Output Example
For rows = 5
, the output will be:
*
**
***
****
*****
For rows = 3
, the output will be:
*
**
***
Conclusion
This Python program prints a right-angled triangle star pattern using a loop to control the number of stars printed in each row. This exercise is a simple yet effective way to practice using loops and handling output formatting in Python.
Comments
Post a Comment
Leave Comment