Python Program for Printing Inverted Hollow Pyramid Star Pattern

Print Inverted Hollow Pyramid Star Pattern

In this Python Program, we will be discussing about how to write a program to print Inverted Hollow Pyramid Star Pattern. In this pattern, there are n number of rows and have to take care first printing a spaces and then printing stars with spaces using for loop with If-Else condition i == 1 or j == 1 or j ==(num*2 -(2*i-1)). So, User have to enter a single value, that will be determine as a number of rows of the pattern. With the help of “Nested For Loop and If-Else Condition” , we will print the Inverted Hollow Pyramid Star Pattern.

Python Program for Printing Inverted Hollow Pyramid Star Pattern

Working:

Step 1. Start

Step 2. Take number of rows as input from the user and stored it into num.

Step 3. Run a loop ‘i’ number of times to iterate through all the rows which is Starting from i=0 to num. 

Step 4. Run a nested loop inside the main loop for printing spaces which is starting from j=0 to i.

Step 5. Run a nested loop inside the main loop for printing stars which is starting from j=num*2 to (num*2 – (2*i – 1))+1.

Step 6. Inside the above loop print stars only if i == 1 or j == 1 or j ==(num*2 -(2*i-1)) in all other cases print a blank space.

Step 7. Move to the next line by printing a new line using print() function.

Step 8. Stop

Python Program:

num = int(input("Enter the Number: "))

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

    for j in range(1, (num*2 - (2*i - 1))+1):
        if i == 1 or j == 1 or j ==(num*2 -(2*i-1)):
            print("*", end="")
        else:
            print(" ", end="")
    print()


# This code is contributed by Shubhanshu Arya (Prepinsta Placement Cell Student)