Diamond pattern in python using while loop

Harshad Koshti
Feb 28, 2021

--

n = int(input("Enter n:"))
m = (n+1)/2

i = 1
while i<=n:
if i<m:
b = m-i
s = 2*i-1
else:
b = i - m
s = 2*(n-i) + 1
j = 1
while j<=b:
print(" ",end="")
j += 1
j = 1
while j<=s:
print("*",end="")
j +=1
print()
i += 1
Output:
n = 11
*
***
*****
*******
*********
***********
*********
*******
*****
***
*

logic :
For upper triangle : blank space = m - i
star : 2*i - 1
For lower triangle: blank space: i -m
star : 2*(n-i) -1

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response