in python
Write a program to print the square of first twenty natural numbers in descending order
Answers
Answered by
3
print("\n".join([str(n ** 2) for n in range(20, 0, -1)]))
Answered by
2
Required Answer:-
Question:
- Write a python program to print the squares of first twenty natural numbers in descending order.
Solution:
This is an easy question. Here is the code.
for i in range(21,0,-1):
print(i**2,end=" ")
This can be solved in 1 line too!! Here is the code.
>> print(" ".join(str(i**2) for i in range(21,0,-1)))
For verification, check out the attachment.
Logic:
- A loop is created that iterates 20 times. After each iteration, you can see that the value of i becomes 20, 19, 18,....1. After each iteration, square of i is displayed on the screen. Only one variable is used (i).
Attachments:
Similar questions