rows = int(input("Enter the number of rows: "))
for i in range(rows, 0, -1):
print("*" * i)
#source code --> clcoding.com
Code Explanation:
1. Input: Number of Rows
rows = int(input("Enter the number of rows: "))
input(): Prompts the user to enter a value.
int(): Converts the input (string) into an integer.
rows: Represents the total number of rows in the reverse triangle.
Example:
If the user inputs 5, then:
rows = 5
2. Reverse Triangle Logic
for i in range(rows, 0, -1):
range(rows, 0, -1):
Starts at rows (e.g., 5).
Stops at 0 (not inclusive).
Decrements by -1 in each iteration, creating a countdown.
i: Represents the current row number. The value of i starts from rows and decreases to 1.
Example:
For rows = 5, the range(rows, 0, -1) produces:
5, 4, 3, 2, 1
3. Print Asterisk Pattern
print("*" * i)
"*" * i:
Repeats the asterisk (*) i times for the current row.
For example, if i = 5, "*" is repeated 5 times (*****).
print():
Outputs the repeated asterisks on a new line, creating one row of the triangle.
0 Comments:
Post a Comment