Python Program to Print an Inverted Star Pattern

This is a Python Program to read a number n and print an inverted star pattern of the desired size.

Problem Description

The program takes a number n and prints an inverted star pattern of the desired size.

Problem Solution

1. Take a value from the user and store it in a variable n.
2. Use a for loop where the value of i ranges between the values of n-1 and 0 with a decrement of 1 with each iteration.
3. Multiply empty spaces with n-i and ‘*’ with i and print both of them.
4. Exit.

Program/Source Code

Here is the source code of the Python Program to read a number n and print an inverted star pattern of the desired size. The program output is also shown below.

n=int(input("Enter number of rows: "))
for i in range (n,0,-1):
    print((n-i) * ' ' + i * '*')
Program Explanation

1. User must first enter the value and store it in a variable n.
2. The for loop enables i to range between n-1 and 0 with a decrement of 1 with each iteration.
3. For each iteration, ” ” is multiplied with n-i and ‘*’ is multiplied with i to ensure correct spacing of the stars.
4. The required pattern is printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter number of rows: 5
*****
 ****
  ***
   **
    *
 
Case 2:
Enter number of rows: 10
**********
 *********
  ********
   *******
    ******
     *****
      ****
       ***
        **
         *

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.