Odd number

The Opposite of even numbers. Odd numbers have a difference of 3 unit or number. In other words, if the number is not completely divisible by 2 then it is an odd number.

Logic

This program is much similar to this one: Python program to print all odd numbers from 1 to N. The only difference here is that instead of printing them we have to add all the odd numbers inside the loop. We will add all the odd numbers in one temporary variable “sum” and after the completion of the loop, we have to print that temporary variable.

See also: Check whether number is even or odd

Program

# Take input from user.
num = int(input("Print sum of odd numbers till : "))
sum = 0;

for i in range(1, num + 1):

    #Check for odd or not.
    if(not (i % 2) == 0):
        sum += i;

print("\nSum of odd numbers from 1 to", num, "is :", sum)

Output

Print sum of odd numbers till : 50
Sum of odd numbers from 1 to 50 is : 625