0% found this document useful (0 votes)
18 views

Python Program To Print Mirror Lower Star Triangle Pattern

Uploaded by

harshit.content
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Python Program To Print Mirror Lower Star Triangle Pattern

Uploaded by

harshit.content
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Harshit Sachan

Python Program to Print Mirror Lower Star Triangle


Pattern
Guido van Rossum developed the high-level, all-purpose
programming language Python, which was first made available
in 1991. It is employed for a variety of purposes, including
system scripting, software development, and server-side web
development.
In this tutorial, we will learn how to write a python program to
print a mirror lower star triangle pattern and for that we need
to know two things in python:
1. Loops in python
2. range()
Python has a wide variety of loop types to accommodate
looping needs.
Loops are used for sequential traversal, which is the looping or
iterating across lists, tuples, etc.
Syntax of for loops:-
For a variable in sequence:
Statement()
It may be utilized to repeat a range or a sequence.
Example:
for i in range(0, 7):
print(i)
output: -
0
1
2
3
4
Harshit Sachan

5
6

Example 2: -
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in lst:
print(i)
output: -
1
2
3
4
5
6
7
8
9
10
While loop: - A while loop is used to repeatedly run a block of
code until a specified condition is met.
Syntax :
while condition:
statement()
example 1: -
i=0
while (i < 3):
Harshit Sachan

i=i+1
print(“Hello, World!”)
output: -
Hello, World!
Hello, World!
Hello, World!

Range(): - To obtain the series of integers between a given


range of numbers, we use the range function.
syntax: -
range( start, stop, step )
Here, start denotes the starting point, stop the terminus, and
step denotes the gaps or jumps that should be between the
values.
The range function can also be used without the start and step
parameters, making them optional.
Example: -
number = range(1, 10, 2)
print(list(number))
output: -
[1, 3, 5, 7, 9]
example: -
number = range(1, 4)
print(list(number))
output: -
[1, 2, 3]
Example: -
number = range(4)
Harshit Sachan

print(list(number))
output: -
[0, 1, 2, 3]
Problem statement: Given a number as input, the challenge
is to print a Mirror Lower Star Triangle Pattern with n rows,
where n is the inputted number.
The crucial thing to bear in mind is that we must print two
pyramids—one facing down and one facing upward—instead of
triangles, which means that in addition to printing stars, we
also need to consider the symmetry and the empty spaces in
both pyramids. This is what makes it substantially more
difficult because we did not have to think about the symmetry
in the case of right and left triangles.
Algorithm:
Start with Step 1
Declare the four integer values I j, k, and my input in step two.
Step 3: Read the user's needed values and define them.
Step 4: To get space between the characters, we run through
two nested "for" loops.
Step 5: After repeating the innermost loop, we repeat the 'for'
loop once more. This will assist in printing the necessary
character.
Step 6: Print a new line to determine how many characters will
appear in the next lines.
Step 7: Show the outcome
Stop at Step 8

Let us say that we are given 5 as input the pattern we will have to print is:

*****

****
Harshit Sachan

***

**

**

***

****

*****

Example 1: The user is entering the information in response to


a prompt.

rows = int(input("Enter the number of rows: "))


k=0
for i in range(rows, 0, -1):
for j in range(k, 0, -1):
print(end=" ")
k=k+1
for j in range(0, i):
print("*", end=" ")
print("")
n = rows-1
for i in range(1, rows+1, 1):
for j in range(0, n):
print(end=" ")
n=n-1
for j in range(0, i, 1):
Harshit Sachan

print("*", end=" ")


print("")

Output:
Enter the number of rows: 8
********
*******
******
*****
****
***
**
*
*
**
***
****
*****
******
*******
********

Explanation: - The number of rows is input into the code above


as a dynamic value. The outer loop is the next phase, and it
iterates from n to 1, decreasing with each iteration. The inner
loop outputs the number of stars equal to the value of the
outer loop's counter variable after each iteration. same, mirror
experiences the opposite
Harshit Sachan

Example 2: The integer has a prior definition.


rows = 6
k=0
for i in range(rows, 0, -1):
for j in range(k, 0, -1):
print(end=" ")
k=k+1
for j in range(0, i):
print("*", end=" ")
print("")
n = rows-1
for i in range(1, rows+1, 1):
for j in range(0, n):
print(end=" ")
n=n-1
for j in range(0, i, 1):
print("*", end=" ")
print("")

output:
******
*****
****
***
**
Harshit Sachan

*
*
**
***
****
*****
******
Explanation: - The number of rows is input into the code above
as a static value. The outer loop is the next phase, and it
iterates from 6 to 1, decreasing with each iteration. The inner
loop outputs the number of stars equal to the value of the
outer loop's counter variable after each iteration. same, mirror
experiences the opposite

You might also like