- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 n representing 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.
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 order from 1 to i.
Print a star at the end of the row.
And finally, call the function with a specific n value to generate the desired output.
Example
Let’s see the following Python program to display a half-diamond pattern of numbers with a star border.
def print_half_diamond_pattern(n): # Upper half for i in range(1, n + 1): # Print stars 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 stars print("*") # Lower half for i in range(n - 1, 0, -1): # Print stars 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 stars print("*") # define n value n = 9 print_half_diamond_pattern(n)
Output
*1* *121* *12321* *1234321* *123454321* *12345654321* *1234567654321* *123456787654321* *12345678987654321* *123456787654321* *1234567654321* *12345654321* *123454321* *1234321* *12321* *121* *1*
Example
In this example, the pattern consists of numbers increasing in each row, without the decreasing numbers in the lower half. The rest of the structure remains the same as in the previous example.
def print_half_diamond_pattern(n): # Upper half for i in range(1, n + 1): # Print stars print("*", end="") # Print numbers in increasing order for j in range(1, i + 1): print(j, end="") # Print stars print("*") # Lower half for i in range(n - 1, 0, -1): # Print stars print("*", end="") # Print numbers in increasing order for j in range(1, i + 1): print(j, end="") # Print stars print("*") # define n value n = 5 print("Half diamond pattern: ") print_half_diamond_pattern(n)
Output
Half diamond pattern: *1* *12* *123* *1234* *12345* *1234* *123* *12* *1*