Write a program in python to find the sum of first 10 even numbers

Even number

Even numbers are numbers that have a difference of 2 unit or number. In other words, if the number is completely divisible by 2 then it is an even number.

Sum of N even numbers

This program is much similar to this one: Print all even numbers from 1 to N. The only difference is that instead of printing them we have to add it to some temporary variable and print it.

Logic

First, we declare one variable ” sum ” with value 0, and then we are going to use this variable to store sum of all even numbers between 1 to N. Now after taking input [N] from user, we have to check if the current variable “i” is even or not inside the loop . If it is even we have to add it to variable ” sum ” otherwise continue with the loop.

Once the loop is completed we have to print the variable ” sum “ containing the sum of all even numbers up to N.

Program

# Take input from user.
num = int[input["Print sum of even numbers till : "]]

total = 0

for i in range[1, num + 1]:

    # Check for even or not.
    if[[i % 2] == 0]:
        total = total + i

print["\nSum of even numbers from 1 to", num, "is :", total]

Output

Print sum of even numbers till : 100
Sum of even numbers from 1 to 100 is : 2550

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 

Chủ Đề