 Search

## Python Program: Find Sum of All Even Numbers

Learn how to write a Python program to calculate the sum of all even numbers. Follow our step-by-step guide and understand the logic behind the program. Enhance your Python programming skills and solve similar mathematical problems with ease. Get started with this comprehensive tutorial today!

Before moving ahead, let’s see another Python Project.

Example: Sum of even numbers using if statement.

```				```
numbers = int(input("Enter a number: "))

sum = 0

for number in range(0, numbers):
if number % 2 == 0:
print(number)
sum = sum + number

print("The sum of total even numbers are:", sum)

#output:

Enter a number: 11
0
2
4
6
8
10
The sum of total even numbers are: 30
```
```

Explanation:

The code begins by prompting the user to enter a number.

The variable sum initialized from 0 stores the total sum of all even numbers.

The for loop iterates over the range of numbers from 0 to the input number (exclusive). Each number is checked within the loop to determine if it is even using the condition number % 2 == 0. If the number is even, it is printed, and its value is added to the sum variable.

Finally, after the loop terminates, the code displays the total sum of the even numbers using the print statement.

Example: Sum of even numbers without using an if statement.

```				```
num = int(input("Enter a number: "))

sum = 0

for numbers in range(0, num, 2):
print(numbers)
sum = sum + numbers

print("The sum of total even numbers are:", sum)

#output:

Enter a number: 11
0
2
4
6
8
10
The sum of total even numbers are: 30
```
```

Explanation:

The explanation is the same as the above example except for one line, i.e., in the range() function, we used step size 2, which determines to omit or skip off the sequence of numbers from the number of step size. This ensures that only even numbers are considered in the loop.

Overall, this code efficiently finds and accumulates the sum of all even numbers up to the given input number, considering only the even numbers by using a step size of 2 in the for loop.

Example: Sum of even numbers using a while loop.

```				```
num = int(input("Enter a number: "))

sum = 0
count = 1

while count <= num:
if count % 2 == 0:
print(count)
sum = sum + count
count += 1

print("The sum of total even numbers are:", sum)

#output:

Enter a number: 10
2
4
6
8
10
The sum of total even numbers are: 30
```
```

Explanation:

The code begins by prompting the user to enter a number.

The variables sum and count are initialized to 0 and 1, respectively. The sum variable will store the sum of the even numbers, while the count variable keeps track of the current number encountered.

The while loop continues iterating if the count is less than or equal to the input number. The current count is checked within each iteration to determine if it is even using the condition count % 2 == 0. If it is even, the number is printed, and its value is added to the sum variable.

After each iteration, the count is incremented by 1 using the count += 1 statement to move on to the next number.

Finally, after the loop terminates, the code displays the total sum of the even numbers using the print statement.

This code calculates the sum of all even numbers up to the given input number using a while loop.

Example: Sum of even numbers using def function.

```				```
def sum_of_even_numbers(numbers):
sum = 0
for number in range(1, numbers):
if number % 2 == 0:
print(number)
sum = sum + number
print("The sum of total even numbers are:", sum)

#function call
sum_of_even_numbers(11)

#output:
2
4
6
8
10
The sum of total even numbers are: 30
```
```

Explanation:

The code defines a function called sum_of_even_numbers that takes a parameter numbers.

The variable sum initialized from 0 stores the total sum of all even numbers.

The for loop iterates over the range of numbers from 1 to the input number (exclusive). Each number is checked within the loop to determine if it is even using the condition number % 2 == 0. If the number is even, it is printed, and its value is added to the sum variable.

Finally, after the loop terminates, the code displays the total sum of the even numbers using the print statement.

Now, the function sum_of_even_numbers is called with the argument 11, which will execute the function and calculate the sum of even numbers up to 11.

### Conclusion

This article provides a clear and concise explanation of how to write a Python program to calculate the sum of all even numbers. The code examples demonstrate different approaches, including using a for loop and a while loop, to achieve the desired result.

It covers the fundamental concepts of iterating over a range of numbers, checking for evenness using the modulus operator, and accumulating the sum of even numbers. It emphasizes the importance of understanding loop structures and conditional statements in solving mathematical problems with Python.

By following the step-by-step explanations and studying the provided code examples, readers can gain a solid understanding of the logic behind calculating the sum of even numbers.

Overall, the article effectively guides readers through implementing a Python program to calculate the sum of even numbers and equips them with the knowledge and skills to apply similar techniques in their coding projects.

Recent Articles