DEV Community

Muhammad Ali
Muhammad Ali

Posted on

Right-half-diamond-pattern

A Python program that prints a right-aligned upper half followed by a left-aligned inverted lower half using repeated numbers and nested loops.
It is just a begining of my python learning series, where i write some python programs to improve my pyton learning and enhance skills.
Code:
The code is:

`cnt = 1
m = int(input("Enter the number you are up to print{numbers should be in range 2-9}:"))
space = m - 1

# Upper half:
for i in range(1, m + 1):
    print(space * " " + cnt * str(i))
    space -= 1
    cnt += 1

# Lower half:
cnt -= 2
for i in range(m - 1, 0, -1):
    print(cnt * str(i))
    cnt -= 1
`
Enter fullscreen mode Exit fullscreen mode

The output will be a half aligned diamond.

Top comments (0)