Computer Science

Write programs to print the following shapes:

   *
  * *
 * * *
  * *
   *

Python

Python Control Flow

16 Likes

Answer

n = 3 # number of rows

# upper half
for i in range(n) :
    for j in range(n, i+1, -1) : 
        print(' ', end = '')
    for k in range(i+1) :
        print('*', end = ' ')
    print()

# lower half
for i in range(n-1) :
    for j in range(i + 1) : 
        print(' ', end = '')
    for k in range(n-1, i, -1) :
        print('*', end = ' ')
    print()

Output

  * 
 * * 
* * *
 * *
  *

Answered By

9 Likes


Related Questions