Sum of Even Numbers using For Loop in Python


This program uses a for loop to iterate over a range of numbers from the starting value entered by the user to the ending value entered by the user. Within the for loop, an if statement is used to check if the current value of x is even. If it is, it is added to a running total stored in the variable "result". Once the for loop has completed, the program prints the final sum of all even numbers within the specified range.

Source Code

print("Sum of Even 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):
    if(x % 2 == 0):
        result=result+x
        
 
print("Sum of even number is ",result)
To download raw file Click Here

Output

Sum of Even Number using for loop
Enter the starting value : 1
Enter the ending value : 15
Sum of even number is  56