Given a number n, the task is to write a Python program to print a half-diamond pattern of numbers with a star border.
Examples:
Input: n = 5
Output:
*
*1*
*121*
*12321*
*1234321*
*123454321*
*1234321*
*12321*
*121*
*1*
*
Input: n = 3
Output:
*
*1*
*121*
*12321*
*121*
*1*
*
Approach:
- Two for loops will be run in this program in order to print the numbers as well as stars.
- First print * and then run for loop from 1 to (n+1) to print up to the rows in ascending order.
- In this particular for loop * will be printed up to i and then one more for loop will run from 1 to i+1 in order to print the numbers in ascending order.
- Now one more loop will run from i-1 to 0 in order to print the number in the reverse order.
- Now one star will be printed and this for loop will end.
- Now second for loop will run from n-1 to 0 to print the pattern as in the middle in which the numbers are in a reverse manner.
- In this for loop also the same work will be done as in first for loop.
- The required pattern will be displayed.
Below is the implementation of the above pattern:
Python3
def display(n):
print ( "*" )
for i in range ( 1 , n + 1 ):
print ( "*" , end = "")
for j in range ( 1 , i + 1 ):
print (j, end = "")
for j in range (i - 1 , 0 , - 1 ):
print (j, end = "")
print ( "*" , end = "")
print ()
for i in range (n - 1 , 0 , - 1 ):
print ( "*" , end = "")
for j in range ( 1 , i + 1 ):
print (j, end = "")
for j in range (i - 1 , 0 , - 1 ):
print (j, end = "")
print ( "*" , end = "")
print ()
print ( "*" )
n = 5
print ( '\nFor n =' , n)
display(n)
n = 3
print ( '\nFor n =' , n)
display(n)
|
Output
For n = 5
*
*1*
*121*
*12321*
*1234321*
*123454321*
*1234321*
*12321*
*121*
*1*
*
For n = 3
*
*1*
*121*
*12321*
*121*
*1*
*
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!