write a Python program to find the sum of the natural number from 21to30
Answers
Answered by
3
The given problem is solved using language - Python.
1. Using loops.
s = 0
for _ in range(21,30 + 1):
s += _
print('Sum of numbers from 21 to 30 is:', s)
2. By creating a list.
s = sum(list(range(21,30 + 1)))
print('Sum of numbers from 21 to 30 is:', s)
- Iterate a loop in the range 21 to 30 (I wrote 31 because last value is excluded).
- Add each numbers present in this range.
- Store the sum in variable 'sum'.
- Display the value of the variable 'sum'.
See attachment for output.
Attachments:
Similar questions