Python Practice › Loop Programs › Program 17

Sum of even numbers 1 to n

EasyLoop Programs · 17 of 90

Write a Python program to find the sum of all even numbers from 1 to n.

Advertisement
Input
n = 20
Output
Sum of even numbers: 110

Approach

  1. Loop over even numbers only and add them.

Solution

Python
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.

More loop programs

← All 90 Loop Programs

Leave a Comment