CBSE Class 11 Computer Science Question 150 of 161

Flow of Control — Question 27

Back to all questions
27
Question

Question 24b

Write programs to print the following shapes:

*
* *
* * *
* *
*

Solution
n = 3 # number of rows

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

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