





Please login
Prime

Prepinsta Prime
Video courses for company/skill based Preparation
(Check all courses)
Get Prime Video
Prime

Prepinsta Prime
Purchase mock tests for company/skill building
(Check all mocks)
Get Prime mock
Python Program to print Diamond Star Pattern | PrepInsta
Diamond Star Pattern Printing
In this program, we are taking the number of rows as input from the user and store in the variable named n, and then we will use two for loops, one for the upper half and the second for the lower half.
In the first half ” * ” increases as the number of rows increases and then in the second half “*” decreases as we go down.

Algorithm
- Take number of Rows as input from the user
- Call diamondStar(n)
diamondStar(rows):
- Initialize two variables k1=1 and k2=rows-1
- Traverse i from 0 to rows and do step 3 to step 8 (1st Outer Loop for the upper half )
- Traverse j from 0 to rows and do step 4 to step 5 (Inner loop to print elements in a particular row)
- if j is greater than or equal to k2 print *
- else print ” ” blank space.
- End of inner loop
- Decrement k for each row
- Print new line character for each inner iteration
- End of 1st outer loop
- Traverse i from 0 to rows and do step 11 to step 16 (2nd Outer Loop for the lower half )
- Traverse j from 0 to rows and do step 12 to step 13 (Inner loop to print elements in a particular row)
- if j is greater than or equal to k1 print *
- else print ” “
- End of inner loop
- Increment k for each row
- Print new line character for each inner iteration
- End of 2nd outer loop.
Python Code
#function to print diamon star pattern
def diamondStar(rows):
#intialize variables k1 and k2
k1=1
k2=rows-1
#1St outer loop for each row in the upper half
for i in range(0,rows):
#Inner loop to print elements in a particular row
for j in range(0,rows):
#if j>=k2 print *
if j>=k2:
print(“* “,end=” “)
#if j<k2 print blank space
else:
print(” “,end=” “)
#decrement k2
k2-=1
#print new line character for each row
print()
#2nd Outer loop for each row in the lower half
for i in range(0,rows):
#Inner loop to print elements in a particular row
for j in range(0,rows):
#if j>=k1 print *
if j>=k1:
print(“* “,end=” “)
#if j<k1 print blank space
else:
print(” “,end=” “)
#increment k1
k1+=1
#print new line character for each row
print()
#input
n=int(input(“Enter the number of Rows : “))
#function call
diamondStar(n)
Output : Enter the number of Rows : 6 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Login/Signup to comment