47. Python Program to find the Sum of n Natural numbers.
How does this program work?
- In this program we are going to learn about how to find the Sum of n natural numbers using Python.
- The integer entered by the user is stored in variable n.
- Declare variable 's' to store the sum of numbers and initialize it with 0.
- By using while loop we can add the sum of n natural numbers.
Here is the code
n = (int(input("Enter the maximum natural value: ")))
i = 1
s = 0
while i <= n:
s+= i #(s = s+i)
i+=1
print("Sum of ",n," natural numbers is: ",s)