Sum of Odd Numbers using For Loop in Python


The program is using a for loop to iterate through a range of numbers starting from the value entered by the user as the starting value, and ending at the value entered by the user as the ending value. It is checking each number in the range to see if it is odd by using the modulus operator to check if the remainder when dividing by 2 is equal to 1. If the number is odd, it is added to a variable called "result" which is initially set to 0. At the end of the for loop, the program will print out the sum of all the odd numbers in the given range.

Source Code

print("Sum of Odd 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 == 1):
        result=result+x
        
print("Sum of odd number is ",result)
To download raw file Click Here

Output

Sum of Odd Number using for loop
Enter the starting value : 1
Enter the ending value : 10
Sum of odd number is  25