How to write a Python Program to Calculate Sum of Even Numbers from 1 to N using While Loop, and For Loop with example.

**TIP: **I suggest you to refer Find Even Numbers from 1 to N article to understand the logic behind printing Even numbers.

## Python Program to Calculate Sum of Even Numbers from 1 to N using For Loop

This Python program allows the user to enter the maximum limit value. Next, it is going to calculate sum of even numbers from 1 to that user entered value.

In this example, we are using For Loop to keep the number between 1 and maximum value.

# Python Program to Calculate Sum of Even Numbers from 1 to N maximum = int(input(" Please Enter the Maximum Value : ")) total = 0 for number in range(1, maximum+1): if(number % 2 == 0): print("{0}".format(number)) total = total + number print("The Sum of Even Numbers from 1 to {0} = {1}".format(number, total))

**OUTPUT**

## Python Program to Calculate Sum of Even Numbers from 1 to N without If Statement

This is same as above, but we altered the for loop to remove If block.

# Python Program to Calculate Sum of Even Numbers from 1 to N maximum = int(input(" Please Enter the Maximum Value : ")) total = 0 for number in range(2, maximum + 1, 2): print("{0}".format(number)) total = total + number print("The Sum of Even Numbers from 1 to {0} = {1}".format(number, total))

**OUTPUT**

## Python Program to find Sum of Even Numbers using While Loop

In this example, we replaced the For Loop with While Loop.

# Python Program to Calculate Sum of Even Numbers from 1 to N maximum = int(input(" Please Enter the Maximum Value : ")) total = 0 number = 1 while number <= maximum: if(number % 2 == 0): print("{0}".format(number)) total = total + number number = number + 1 print("The Sum of Even Numbers from 1 to N = {0}”.format(total))

**OUTPUT**

## Python Program to find Sum of Even Numbers from 1 to 100

This program allows user to enter Minimum and maximum value. Next, it will calculate sum of even numbers from Minimum to maximum value.

# Python Program to Calculate Sum of Even Numbers from 1 to 100 minimum = int(input(" Please Enter the Minimum Value : ")) maximum = int(input(" Please Enter the Maximum Value : ")) total = 0 for number in range(minimum, maximum+1): if(number % 2 == 0): print("{0}".format(number)) total = total + number print("The Sum of Even Numbers from {0} to {1} = {2}".format(minimum, number, total))

**OUTPUT**