Don’t worry, unlock all articles / blogs on PrepInsta by just simply logging in on our website
Python Program for Printing Diamond Star Pattern
June 5, 2020
Print Diamond Star Pattern
In this Python Program, we will be discussing about how to write a program to print Diamond Star Pattern. In this pattern, there are n rows with i numbers of time of iterations through all the rows. In columns, num-i-1 numbers for printing Spaces and i*2+1 numbers for printing stars. Run another loop with i(Starts from 1) numbers of time of iterations through all the rows. In columns, i numbers for printing Spaces and (num-i)*2-1 numbers for printing stars. So, User have to enter a single value, that will be determine as a number of rows of the pattern. With the help of “Two Different Different Nested For Loop” , we will print the Diamond 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 num-i-1.
Step 5. Run a nested loop inside the main loop for printing stars which is starting from j=0 to i*2+1.
Step 6. Move to the next line by printing a new line using print() function.
Step 7. Run a loop ‘i’ number of times to iterate through all the rows which is Starting from i=1 to num.
Step 8. Run a nested loop inside the main loop for printing spaces which is starting from j=0 to i.
Step 9. Run a nested loop inside the main loop for printing stars which is starting from j=0 to (num-i)*2-1.
Stop 10. Move to the next line by printing a new line using print() function.
Step 11. Stop
Python Program:
num = int(input("Enter the Number: "))
for i in range(0, num):
for j in range(0, num-i-1):
print(" ",end="")
for j in range(0, i*2+1):
print("*", end="")
print()
for i in range(1, num):
for j in range(0, i):
print(" ",end="")
for j in range(0, (num-i)*2-1):
print("*", end="")
print()
# This code is contributed by Shubhanshu Arya (Prepinsta Placement Cell Student)
#python
n=int(input())
sum=1
l=[1]
for i in range(0,n):
print(” “*(n-1-i)+”*”*(sum))
sum=sum+2
l.append(sum)
for i in range(n-1,0,-1):
print(” “*(n-i)+”*”*l[i-1])
for i in range(rows-1):
for j in range(i,rows):
print(” “, end=” “)
for j in range(i):
print(“*”, end=” “)
for j in range(i+1):
print(“*”, end=” “)
print()
for i in range(rows):
for j in range(i+1):
print(” “, end=” “)
for j in range(i+1,rows):
print(“*”, end=” “)
for j in range(i,rows):
print(“*”, end=” “)
print()
num=5
for i in range(num):
for j in range(num-i-1):
print(” “,end=””)
for j in range(i*2+1):
print(“*”,end=””)
print()
for i in range(num-1,0,-1):
for j in range(num-i,0,-1):
print(” “,end=””)
for j in range(i*2-1,0,-1):
print(“*”,end=””)
print()
n=int(input())
for i in range(1,n+1):
for j in range(n-i):
print(” “,end=””)
for j in range(i):
print(“*”,end=” “)
print()
for i in range(1,n+1):
for j in range(i):
print(” “,end=””)
for j in range(n-i):
print(“*”,end=” “)
print()
#python
n=int(input())
sum=1
l=[1]
for i in range(0,n):
print(” “*(n-1-i)+”*”*(sum))
sum=sum+2
l.append(sum)
for i in range(n-1,0,-1):
print(” “*(n-i)+”*”*l[i-1])
Hey, our mentors are available on Discord for helping you in technical questions <3
rows = int(input(“Enter the number of rows: “))
for i in range(rows-1):
for j in range(i,rows):
print(” “, end=” “)
for j in range(i):
print(“*”, end=” “)
for j in range(i+1):
print(“*”, end=” “)
print()
for i in range(rows):
for j in range(i+1):
print(” “, end=” “)
for j in range(i+1,rows):
print(“*”, end=” “)
for j in range(i,rows):
print(“*”, end=” “)
print()
num=5
for i in range(num):
for j in range(num-i-1):
print(” “,end=””)
for j in range(i*2+1):
print(“*”,end=””)
print()
for i in range(num-1,0,-1):
for j in range(num-i,0,-1):
print(” “,end=””)
for j in range(i*2-1,0,-1):
print(“*”,end=””)
print()
n=int(input())
for i in range(1,n+1):
for j in range(n-i):
print(” “,end=””)
for j in range(i):
print(“*”,end=” “)
print()
for i in range(1,n+1):
for j in range(i):
print(” “,end=””)
for j in range(n-i):
print(“*”,end=” “)
print()
n=int(input())
for i in range(1,n):
print(” “*(n-i)+”*”*(i+i-1))
for i in range(0,n):
print(” “*(i)+”*”*((n+n-1)-i*2))