Program to find sum of even numbers in python

In this post, you will learn how to write a Python program to get the sum of even numbers. There are different ways to calculate the sum of even numbers. Here we have mentioned most of them-

Python program to calculate sum of even numbers using for loop

In the given program, we first take user input to enter the maximum limit value. Then, we have used the for loop to calculate the sum of even numbers from 1 to that user-entered value.

# Python Program to Calculate
# Sum of Even Numbers from 1 to N
 
max = int(input("Please enter the maximum value: "))
total = 0

for num in range(1, max+1):
    if(num % 2 == 0):
        print("{0}".format(num))
        total = total + num

print("The Sum of Even Numbers from 1 to {0} = {1}".format(num, total))

Output of the above code:

Please enter the maximum value: 23
2
4
6
8
10
12
14
16
18
20
22
The Sum of Even Numbers from 1 to 23 = 132

Python program to calculate sum of even numbers using for loop without If Statement

In the given program, first we have taken user input to enter the maximum limit value. Then, we have used the for loop to calculate the sum of even numbers from 1 to that user-entered value without using if statement.

# Python program to calculate sum of even numbers 
# from 1 to N
 
max_num = int(input("Please enter the maximum value : "))
total = 0

for num in range(2, max_num + 1, 2):
    print("{0}".format(num))
    total = total + num

print("The Sum of Even Numbers from 1 to {0} = {1}".format(num, total))

Output of the above code

Please enter the maximum value : 20
2
4
6
8
10
12
14
16
18
20
The Sum of Even Numbers from 1 to 20 = 110

Python program to calculate sum of even numbers using while loop

In the given program, we have applied the same logic as above, just replaced the for loop with a while loop.

# Python program to calculate
# sum of even numbers from 1 to N
 
max = int(input("Please enter the maximum value:"))
total = 0
num = 1
 
while num <= max:
    if(num % 2 == 0):
        print("{0}".format(num))
        total = total + num
    num = num + 1

print("The sum of even numbers from 1 to N = {0}".format(total))

Output:

Please enter the maximum value:20
2
4
6
8
10
12
14
16
18
20
The sum of even numbers from 1 to N = 110

Pranavi Anoushka Tirumalasetty

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

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)

CONTRIBUTOR

Pranavi Anoushka Tirumalasetty

Python program to get input n and calculate the sum of even numbers till n

Sample Input 1:

5

Sample Output 1:

6(2+4)

Program or Solution

				
			
					
n=int(input("Enter n value:"))
sum=0
for i in range(2,n+1,2):
    sum+=i
print(sum)
    

			
				
			

Program Explanation

For Statement is used to execute the sequence of instruction repeatedly.

Range() method gives list of elements, here range() method gives list which has 2,4,6... to n or n-1.

for statement executes the instructions iteratively and for takes the elements one by one as value of i in sequential manner.

so it adds even numbers.

The following article shows how given an integer list, we can produce the sum of all its odd and even digits.

Input : test_list = [345, 893, 1948, 34, 2346] 
Output : 
Odd digit sum : 36 
Even digit sum : 40 
Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.
Input : test_list = [345, 893] 
Output : 
Odd digit sum : 20 
Even digit sum : 12 
Explanation : 4 + 8 = 12, even summation. 

Method 1 : Using loop, str() and int()

In this, we first convert each element to string and then iterate for each of its element, and add to respective summation by conversion to integer.

Python3

test_list = [345, 893, 1948, 34, 2346]

print("The original list is : " + str(test_list))

odd_sum = 0

even_sum = 0

for sub in test_list:

    for ele in str(sub):

        if int(ele) % 2 == 0:

            even_sum += int(ele)

        else:

            odd_sum += int(ele)

print("Odd digit sum : " + str(odd_sum))

print("Even digit sum : " + str(even_sum))

Output

The original list is : [345, 893, 1948, 34, 2346]
Odd digit sum : 36
Even digit sum : 40

Method 2: Using loop and sum()

In this, we perform task of getting summation using sum(), and loop is used to perform the task of iterating through each element.

Python3

test_list = [345, 893, 1948, 34, 2346]

print("The original list is : " + str(test_list))

odd_sum = 0

even_sum = 0

for sub in test_list:

    odd_sum += sum([int(ele) for ele in str(sub) if int(ele) % 2 == 1])

    even_sum += sum([int(ele) for ele in str(sub) if int(ele) % 2 == 0])

print("Odd digit sum : " + str(odd_sum))

print("Even digit sum : " + str(even_sum))

Output

The original list is : [345, 893, 1948, 34, 2346]
Odd digit sum : 36
Even digit sum : 40

Method 3: Using list comprehension 

Python3

test_list = [345, 893, 1948, 34, 2346]

odd_sum = 0

even_sum = 0

odd_sum += sum([int(ele)

                for sub in test_list for ele in str(sub) if int(ele) % 2 == 1])

even_sum += sum([int(ele)

                 for sub in test_list for ele in str(sub) if int(ele) % 2 == 0])

print("Odd digit sum : " + str(odd_sum))

print("Even digit sum : " + str(even_sum))

Output

Odd digit sum : 36
Even digit sum : 40

Method 4: Using the enumerate function

Python3

test_list = [345, 893, 1948, 34, 2346]

odd_sum = 0

even_sum = 0

odd_sum += sum([int(ele) for i, sub in enumerate(test_list)

                for ele in str(sub) if int(ele) % 2 == 1])

even_sum += sum([int(ele) for i, sub in enumerate(test_list)

                 for ele in str(sub) if int(ele) % 2 == 0])

print("Odd digit sum : " + str(odd_sum))

print("Even digit sum : " + str(even_sum))

Output

Odd digit sum : 36
Even digit sum : 40


How do you find the sum of all even numbers in Python?

Python program to calculate sum of even numbers using for loop without If Statement. In the given program, first we have taken user input to enter the maximum limit value. Then, we have used the for loop to calculate the sum of even numbers from 1 to that user-entered value without using if statement.

How do you find the sum of the first n even numbers in Python?

Python program to find sum of first n even numbers The range function is used to find the sum between the range of the numbers given by the user as the input. The if((i % 2) == 0) is used to check the given number is even number. If the number is even then sum = sum + i is used to add the numbers.

How do you find the sum of even numbers?

The sum of even numbers formula is obtained by using the sum of terms in an arithmetic progression formula. The formula is: Sum of Even Numbers Formula = n(n+1) where n is the number of terms in the series.

How do you print the sum of even and odd numbers in Python?

Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation. Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.