Sum of All Odd Numbers Up to N

Print the sum of all odd numbers up to n.

Logic BuildingBeginner
Logic Building
# Take n as input
n = int(input("Enter n: "))

# Calculate sum of odd numbers
sum_odd = 0
for i in range(1, n + 1, 2):
    sum_odd += i

print(f"Sum of odd numbers up to {n}: {sum_odd}")

Output

Enter n: 10
Sum of odd numbers up to 10: 25

Sum only odd numbers using step 2.

Key Concepts:

  • Start from 1 (first odd number)
  • Step by 2 to get only odd numbers
  • Accumulate sum