Competitive programming in Python |
In this article, we are going to write a Python Program to print out the diamond shape of stars.
If we break down the diamond shape then it's a combination of an equilateral triangle (vertical and inverted). So, we are going to write down the code accordingly.
If you are looking for a Python Tutorial Series, then it's here.
def diamond(n):
stars = 1
spaces = n-1
stars2 = 2*n-3
spaces2 = 1
for i in range(1, n+1):
print(' '*spaces + '*'*stars)
stars += 2
spaces -= 1
for i in range(1, n+1):
print(' ' * spaces2 + '*' * stars2)
stars2 -=2
spaces2 +=1
diamond(6)
The output of the above program is:
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
Comments
Post a Comment