0
0
PythonProgramBeginner · 2 min read

Python Program to Find Sum of Odd Numbers

You can find the sum of odd numbers in Python by using a loop and adding numbers that satisfy num % 2 != 0, for example: sum_odd = sum(x for x in range(1, n+1) if x % 2 != 0).
📋

Examples

Input5
Output9
Input10
Output25
Input1
Output1
🧠

How to Think About It

To find the sum of odd numbers up to a given number, you check each number from 1 to that number and add it to the total only if it is odd. Odd numbers are those that leave a remainder when divided by 2.
📐

Algorithm

1
Get the input number n
2
Initialize sum to 0
3
For each number from 1 to n, check if it is odd using the condition number % 2 != 0
4
If odd, add the number to sum
5
After the loop ends, return or print the sum
💻

Code

python
n = int(input("Enter a number: "))
sum_odd = 0
for num in range(1, n + 1):
    if num % 2 != 0:
        sum_odd += num
print("Sum of odd numbers:", sum_odd)
Output
Enter a number: 5 Sum of odd numbers: 9
🔍

Dry Run

Let's trace the input 5 through the code

1

Input

n = 5

2

Initialize sum

sum_odd = 0

3

Loop start

num = 1, check 1 % 2 != 0 (True), sum_odd = 0 + 1 = 1

4

Next iteration

num = 2, check 2 % 2 != 0 (False), sum_odd = 1

5

Next iteration

num = 3, check 3 % 2 != 0 (True), sum_odd = 1 + 3 = 4

6

Next iteration

num = 4, check 4 % 2 != 0 (False), sum_odd = 4

7

Next iteration

num = 5, check 5 % 2 != 0 (True), sum_odd = 4 + 5 = 9

8

Loop end

sum_odd = 9

numnum % 2 != 0sum_odd
1True1
2False1
3True4
4False4
5True9
💡

Why This Works

Step 1: Check each number

The code checks every number from 1 to n to see if it is odd using num % 2 != 0.

Step 2: Add odd numbers

Only numbers that are odd are added to the running total sum_odd.

Step 3: Final sum

After checking all numbers, the total sum of odd numbers is printed.

🔄

Alternative Approaches

Using list comprehension and sum()
python
n = int(input("Enter a number: "))
sum_odd = sum([x for x in range(1, n+1) if x % 2 != 0])
print("Sum of odd numbers:", sum_odd)
This method is shorter and uses Python's built-in functions but may use more memory for large n.
Using arithmetic formula for sum of first k odd numbers
python
n = int(input("Enter a number: "))
k = (n + 1) // 2
sum_odd = k * k
print("Sum of odd numbers:", sum_odd)
This method is very fast and uses math but only works if you want sum of odd numbers from 1 to n.

Complexity: O(n) time, O(1) space

Time Complexity

The program loops through all numbers from 1 to n once, so it takes linear time O(n).

Space Complexity

It uses a fixed amount of extra space for the sum variable, so space complexity is O(1).

Which Approach is Fastest?

The arithmetic formula approach is fastest with O(1) time, while looping and list comprehension are O(n).

ApproachTimeSpaceBest For
Loop with if conditionO(n)O(1)Simple and clear for beginners
List comprehension with sum()O(n)O(n)Concise code but uses more memory
Arithmetic formulaO(1)O(1)Fastest for sum of first k odd numbers
💡
Use num % 2 != 0 to check if a number is odd in Python.
⚠️
Forgetting to include the last number n in the range or using num % 2 == 0 which checks for even numbers instead of odd.