Python Practice › Loop Programs › Program 17
Sum of even numbers 1 to n
Write a Python program to find the sum of all even numbers from 1 to n.
Input
n = 20
Output
Sum of even numbers: 110
Approach
- Loop over even numbers only and add them.
Solution
n = 20total = 0for i in range(2, n + 1, 2): total += iprint("Sum of even numbers:", total)Output
Sum of even numbers: 110
Line by line
Line 3
The step of 2 skips odd numbers entirely.
Learn the concept
New to this topic? Read the lesson, then come back.