Sum of Natural Numbers using For Loop in Python


This program calculates the sum of all natural numbers between the given starting value and ending value using a for loop. The user is prompted to enter the starting value and ending value. The for loop iterates through the range of values between the starting value and ending value (inclusive). Within the loop, a variable called "result" is initialized to zero, and the current value of x is added to it. The final sum is printed out as "Sum of natural number is [result]" after the loop is completed.

Source Code

print("Sum of Natural Number using for loop ")

start = int(input("Enter the starting value : "))
end   = int(input("Enter the ending value : "))

result=0

for x in range(start,end+1):
    result=result+1

print("Sum of natural number is ",result)
To download raw file Click Here

Output

Sum of Natural Number using for loop
Enter the starting value : 1
Enter the ending value : 10
Sum of natural number is  10