Python | Print an Inverted Star Pattern
Last Updated :
24 Aug, 2023
An inverted star pattern involves printing a series of lines, each consisting of stars (*) that are arranged in a decreasing order. Here we are going to print inverted star patterns of desired sizes in Python
Examples:
1) Below is the inverted star pattern of size n=5
(Because there are 5 horizontal lines
or rows consist of stars).
*****
****
***
**
*
2) Below is the inverted star pattern of size n=10
(Because there are 5 horizontal lines
or rows consist of stars).
**********
*********
********
*******
******
*****
****
***
**
*
The provided Python 3 code generates an inverted star pattern with variable n representing the number of rows. The code iterates through a loop, decrementing i from n to 1. For each iteration, the print function is used to generate the pattern. It multiplies (n-i) with space (‘ ‘) and i with asterisk (‘*’) to create the appropriate number of spaces before the stars. This leads to an inverted triangle pattern where the number of stars in each row decreases while the spaces before them increase, creating the desired inverted star pattern.
Let’s see Python program to print inverted star pattern:
Python3
n = 11
for i in range (n, 0 , - 1 ):
print ((n - i) * ' ' + i * '*' )
|
Output
***********
**********
*********
********
*******
******
*****
****
***
**
*
Time complexity: O(n) for given input n
Auxiliary Space: O(1)
Example2: Using Recursion
Here we will implement the inverted star pattern using Recursion.
Python3
def inverted_star_pattern_recursive(height):
if height > 0 :
print ( "*" * height)
inverted_star_pattern_recursive(height - 1 )
height = 5
inverted_star_pattern_recursive(height)
|
Output:
*****
****
***
**
*
Please Login to comment...