Computer Science
Write programs to print the following shapes:
*
* *
* *
* *
* *
* *
*
Python
Python Control Flow
12 Likes
Answer
n = 4 # number of row
#upper half
for i in range(1, n+1) :
#while loop for * and spaces
x = 1
while x < 2 * i :
if x == 1 or x == 2 * i - 1 :
print('*', end = '')
else :
print(' ', end = '')
x += 1
print()
#lower half
for i in range(n-1, 0, -1) :
#while loop for * and spaces
x = 1
while x < 2 * i :
if x == 1 or x == 2 * i - 1 :
print('*', end = '')
else :
print(' ', end = '')
x += 1
print()
Output
*
* *
* *
* *
* *
* *
*
Answered By
7 Likes
Related Questions
Write programs using nested loops to produce the following patterns:
A
B B
C C C
D D D D
E E E E EWrite programs using nested loops to produce the following patterns:
A
A B
A B C
A B C D
A B C D E
A B C D E FWrite programs to print the following shapes:
*
* *
* * *
* *
*Write programs to print the following shapes:
*
* *
* *
* *
*