











Python Program to print Diamond Number Pattern
Diamond Number Pattern Printing
In this section we will learn how to print Diamond Number pattern using Python.
In this program we will take number of rows as user input. Here we will use two for loops 1st loop for printing the upper half of diamond and 2nd loop for printing the lower half of the diamond. There is a slight difference from Star Diamond printing that here we will print rowth number instead of star.


Algorithm
- Take number of Rows as input from the user
- Call diamondNumber(n)
diamondNumber (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 i+1
- else print ” “
- 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 i+1
- 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 diamondNumber(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 numbers
if j>=k2:
print(i+1,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 numbers
if j>=k1:
print(rows-i-1,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
diamondNumber(n)
Output : Enter the number of Rows : 6 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1
Login/Signup to comment