How to compute the sum of even numbers

Sum of even numbers

We can run this program using for loop, while loop, or do while to find the sum of even numbers within a given range of numbers.

In general, even numbers are those numbers that are divisible by 2.

The following can be used to write the logic, as shown as below:

# computing the sum of even numbers
sum = 0
for i in range(10):
 if i % 2 == 0:
   sum = sum + i

For a given even number, we can get its subsequent even number by adding 2 in it.

The following can be used to write the logic in the while loop, as shown below:

# computing the sum of even numbers
sum = 0
i = 0
while(i < 11):
   sum = sum + i
   i = i + 2
Iteration of the loop
Iteration of the loop

The two ways of writing the code to determine the sum of integers in Python language are given below:

sum=0
for i in range(15):
if i%2==0:
sum=sum+i
print("sum =",sum)
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources