Created
January 20, 2025 20:09
-
-
Save tushortz/f07b8ae95b710cdcc9435373d69f35d9 to your computer and use it in GitHub Desktop.
Number diamond generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Number of rows | |
rows = 4 | |
# Upper Triangle | |
k = 2 * rows - 2 | |
num = 0 | |
# Outer loop to handle number of rows | |
for i in range(rows): | |
# Inner loop to handle number of spaces | |
for j in range(k): | |
print(end=" ") | |
k = k - 1 | |
# Inner loop to print patterns | |
for j in range(0, i + 1): | |
num += 1 | |
print(num, end=" ") | |
print("") | |
# Lower Triangle | |
k = rows - 2 | |
# Outer loop to handle number of rows | |
for i in range(rows, -1, -1): | |
# Inner loop to handle number of spaces | |
for j in range(k, 0, -1): | |
print(end=" ") | |
k = k + 1 | |
# Inner loop to print patterns | |
for j in range(0, i + 1): | |
num += 1 | |
print(num, end=" ") | |
print("") | |
### ----- Output ----- ### | |
# 1 | |
# 2 3 | |
# 4 5 6 | |
# 7 8 9 10 | |
# 11 12 13 14 15 | |
# 16 17 18 19 | |
# 20 21 22 | |
# 23 24 | |
# 25 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment