Print Downward Triangle Star Pattern using Python #shorts

Posted by

Python Program to Print Downward Triangle Star Pattern

#shorts #youtubeshorts #shortsvideo

In this article, we will discuss how to write a Python program to print a downward triangle star pattern using loops.

Here is the Python program:

num = 5

for i in range(num, 0, -1):
    for j in range(num-i):
        print(" ", end="")
    for j in range(2*i-1):
        print("*", end="")
    print()

Explanation of the program:

  • We start by defining a variable num with the desired number of rows for the triangle.
  • We use a nested loop to iterate over the rows and columns of the triangle.
  • The first inner loop is used to print spaces before the stars to create the downward triangle shape.
  • The second inner loop is used to print the stars in each row, based on the row number.

When you run this program, it will output the following downward triangle star pattern:

*****
 ***
  *

That’s it! You have successfully created a Python program to print a downward triangle star pattern using loops.