Computer Science, asked by nikhilkumar4567b94, 2 months ago

write a Python program to find the sum of the natural number from 21to30​

Answers

Answered by anindyaadhikari13
3

\texttt{\textsf{\large{\underline{Solution}:}}}

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)

\texttt{\textsf{\large{\underline{Logic}:}}}

  • 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