In this example, we will learn to create a diamond shape using n numbers in python. Here, we will develop the shape of a diamond with 2n rows.
Our main motive is to design a diamond shape pattern using star symbols and print across the lines.
-
Algorithm:
- Input the number of row that is needed to develop a diamond shape pattern
- Use for loop with range(n)
- Use another for loop with range(1,int((n/2))-i+3)
- Print(sep=” “,end=” “)
- Loop(3) ends
- Using for loop for range(1,i+2)
- Print(“*”, end=” “)
- Loop(6) ends
- Print the space and loop (2) ends
- For lower half of the diamond we use for loop with range(n)
- Use another for loop with range(1,5-(int((n/2))-i+3)+2)
- Print(sep=” “,end=” “)
- Loop(11) ends
- Use for loop with range(1,5-i)
- Print(“*”, end=” “) and loop(14) ends
- Print the space and loop(10) ends
- Diamond pattern is shown on the screen
- Exit
Code:
n=int(input("enter the number of rows"))
for i in range(n):
for j in range(1,int((n/2))-i+3):
print(sep=" ",end=" ")
for k in range(1,i+2):
print("*", end=" ")
print()
for i in range(n):
for j in range(1,5-(int((n/2))-i+3)+2):
print(sep=" ",end=" ")
for k in range(1,5-i):
print("*", end=" ")
print()
Output:
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Report Error/ Suggestion