Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to display half diamond pattern of numbers with star border
A half-diamond pattern is a geometric pattern that resembles the shape of a diamond, but only covers half of the diamond.
Diamond patterns can be created using loops in programming. By controlling the loops and the number of characters printed in each row, we can modify the pattern to achieve different shapes and arrangements.
In this article, we will write a Python program that displays a half-diamond pattern of numbers with a star border.
Input Output Scenarios
Let's explore some input-output scenarios for displaying the half-diamond pattern of numbers with a star border.
Scenario 1
Input: n = 3 Output: *1* *121* *12321* *121* *1*
Scenario 2
Input: n = 5 Output: *1* *121* *12321* *1234321* *123454321* *1234321* *12321* *121* *1*
Approach
We can follow the below steps to display the half-diamond pattern of numbers with a star border:
Define a function that takes an input
nrepresenting the number of rows in the pattern.-
Start with the upper half of the pattern:
Use a for loop to iterate over the range from 1 to n + 1 (inclusive).
For each iteration, print a star (*) at the beginning of the row.
Use nested for loops to print the numbers in increasing order from 1 to i, then decreasing order from i-1 to 1.
Print a star at the end of the row.
-
Move to the lower half of the pattern:
Use a for loop to iterate over the range from n - 1 to 0 (exclusive), with a step of -1.
For each iteration, print a star at the beginning of the row.
Use nested for loops to print the numbers in increasing and decreasing order.
Print a star at the end of the row.
Finally, call the function with a specific n value to generate the desired output.
Method 1: Full Diamond Pattern with Mirrored Numbers
This approach creates a pattern where numbers increase up to the row number, then decrease back to 1 in the same row ?
def print_half_diamond_pattern(n):
# Upper half
for i in range(1, n + 1):
# Print opening star
print("*", end="")
# Print numbers in increasing order
for j in range(1, i + 1):
print(j, end="")
# Print numbers in decreasing order
for j in range(i - 1, 0, -1):
print(j, end="")
# Print closing star
print("*")
# Lower half
for i in range(n - 1, 0, -1):
# Print opening star
print("*", end="")
# Print numbers in increasing order
for j in range(1, i + 1):
print(j, end="")
# Print numbers in decreasing order
for j in range(i - 1, 0, -1):
print(j, end="")
# Print closing star
print("*")
# Test with n = 5
n = 5
print_half_diamond_pattern(n)
*1* *121* *12321* *1234321* *123454321* *1234321* *12321* *121* *1*
Method 2: Simple Increasing Number Pattern
In this variation, each row contains only increasing numbers without the mirror effect ?
def print_simple_diamond_pattern(n):
# Upper half
for i in range(1, n + 1):
# Print opening star
print("*", end="")
# Print numbers in increasing order only
for j in range(1, i + 1):
print(j, end="")
# Print closing star
print("*")
# Lower half
for i in range(n - 1, 0, -1):
# Print opening star
print("*", end="")
# Print numbers in increasing order only
for j in range(1, i + 1):
print(j, end="")
# Print closing star
print("*")
# Test with n = 4
n = 4
print("Simple half diamond pattern:")
print_simple_diamond_pattern(n)
Simple half diamond pattern: *1* *12* *123* *1234* *123* *12* *1*
Comparison
| Method | Pattern Type | Example Row (n=3) |
|---|---|---|
| Method 1 | Mirrored numbers | *12321* |
| Method 2 | Increasing only | *123* |
Conclusion
Half-diamond patterns with star borders can be created using nested loops. Method 1 creates symmetric mirrored numbers, while Method 2 uses simple increasing sequences. Both approaches demonstrate how loop control structures can generate complex visual patterns.
