Basic Programs

If Else Programs

While loop Programs

For loop Programs

List Programs


Python Program to Print Sum of all Even Numbers from Given Range using While Loop


In this example shows, how to sum of all even numbers in given range using while loop.

Steps
  1. Get the input range from user
  2. The program calculates the sum of the all even numbers in given range of using a while loop
Example:
Example.py
limit = int(input("Enter the Limit : "))
tot = 0
a = 1
 
while a <= limit:
    if(a % 2 == 0):
        print("{0}".format(a))
        tot = tot + a
    a = a + 1

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

Output

Enter the Limit : 10
2
4
6
8
10
The Sum of Even Numbers from 1 to 10 = 30

Prev Next