Issue
The main question asks to display an inverted right angled number pyramid by using "while loops" but the use of nested loops is prohibited and the numbers should be from 1 to 5
The image above is the pyramid that needs to be printed by using loops but use of nested loops is prohibited There is a hint provided too which needs to be used
Hint - Here, you gotta loop in the range 1 to 6. With this, you'll also need an extra variable, like count = 5. So in loop 1, print('1 '*count) will give 1 1 1 1 1. And remember to decrease the count by 1 inside the loop!
Solution
Here, the first line has 1 and it repeats 5 times, next line, it has 2 and it repeats 4 times, 3rd line has 3 and it repeats 3 times and so on. So the pattern is, at i^th line, it has i, (6-i) times. So design a loop which will print in such way. Your code:
i=1
while i<=5:
print((6-i)*str(i))
i+=1
Answered By - Abhijit Shirwal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.