Given an integer n, the task is to write a python program to print diamond using loops and mathematical formulations. The minimum value of n should be greater than 4.
Examples :
For size = 5
* * *
* * * * *
* * *
*
For size = 8
* * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
For size = 11
* * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Approach :
The following steps are used :
- Form the worksheet of (size/2+2) x size using two loops.
- Apply the if-else conditions for printing stars.
- Apply else condition for rest spaces.
Below is the implementation of the above approach :
Example 1:
Python3
size = 8
spaces = size
for i in range (size / / 2 + 2 ):
for j in range (size):
if j < i - 1 :
print ( ' ' , end = " " )
elif j > spaces:
print ( ' ' , end = " " )
elif (i = = 0 and j = = 0 ) | (i = = 0 and j = = size - 1 ):
print ( ' ' , end = " " )
else :
print ( '*' , end = " " )
spaces - = 1
print ()
|
Output :
* * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
Example 2:
Python3
size = 11
spaces = size
for i in range (size / / 2 + 2 ):
for j in range (size):
if j < i - 1 :
print ( ' ' , end = " " )
elif j > spaces:
print ( ' ' , end = " " )
elif (i = = 0 and j = = 0 ) | (i = = 0 and j = = size - 1 ):
print ( ' ' , end = " " )
else :
print ( '*' , end = " " )
spaces - = 1
print ()
|
Output :
* * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
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!