4 Python examples to print all even numbers in a given range

Python 3 program to print all even numbers in a range:

In this example, we will learn how to print all the even numbers in a given range in 4 different ways. An even number is a number that is perfectly divisible by 2 or the remainder is 0 if you divide that number by 2. To find out all even numbers in a given range, we will take the start and the end values as inputs from the user.

We will ask the user to enter these values. The program will iterate over the numbers in the given range one by one. If an even number is found, it will print it to the user.

We can use the modulo operator, % to check if a number is even or not. The modulo operation, a%b returns the remainder of a/b. So, for any even number n, the value of n%2 will be always 0.

Steps to find even numbers in a range :

  1. Take the lower limit and the upper limit as inputs from the user. Store the values in two separate variables.
  2. Run one loop from the lower limit to the upper limit. For example, if the lower limit is 2 and the upper limit is 10, it will run from 2 to 10.
  3. Check if the number is divisible by 2 or not on each iteration of the loop.
  4. If it is divisible by 2, print out the number. Else, move to the next iteration.
  5. Exit when the loop is completed.

We will learn four different ways. The first two methods will use the above steps and the last two methods will use a slightly different optimized approach to find the even numbers.

Method 1: Python 3 Program to find the even numbers with a for loop:

The following program will find all the even numbers in the range of lower_limit and upper_limit.

lower_limit = int(input("Enter the lower limit : "))
upper_limit = int(input("Enter the upper limit : "))

for i in range(lower_limit,upper_limit+1):
  if(i%2 == 0):
    print(i)

You can also download the above program from Github.

python print even numbers in range

Sample Output:

python print even numbers in range

Explanation:

  1. The input() function is used to read the user-input value. As this is a number, it is converted to an integer by using the int(input()) function. The input() function returns the user-entered data as a string. We are using the int() function to convert the string to an integer. Note that the user-entered value should be an integer, or else it will throw one exception.

  2. As explained above, we need to store the lower and the upper range or limit in two different variables to iterate between them. In this example program, the lower limit is assigned to the lower_limit variable and the upper limit is assigned to the upper_limit variable.

  3. We are using a for loop to iterate between the numbers. You can also use one while loop.

  4. The loop statement for i in range(lower_limit,upper_limit+1) indicates that the loop will run from lower_limit to upper_limit. e.g. if the lower_limit is 1 and the upper_limit is 3, the loop will run for i=1, i=2 and i=3.

  5. Inside the loop, we are checking if the number is divisible by 2 or not. We are using the modulo operator, i%2 to check if i is an even number or odd. If the value of i is perfectly divisible by 2, it will return ‘0’. The modulo operator is the easiest way to test if a number is perfectly divisible by another number or not.

  6. If the current value of i is perfectly divisible by 2, print out its value. Else move to the next iteration.

Method 2: By using a while loop:

We can use a while loop to find out all the even numbers in a given range. The syntax of the while loop is:

while(condition):
  # code block

It checks one condition and executes its body until the condition is True. We can use a while loop to write the program as below:

lower = int(input("Enter the lower range: "))
upper = int(input("Enter the upper range: "))

while lower <= upper:
    if lower % 2 == 0:
        print(lower)
    lower += 1

Download the code on Github

  • The while loop will run until the value of lower is smaller or equal to upper.
  • On each step, it checks if the value of lower is even or not. If yes, it prints its value.
  • At the end of each iteration, it is incrementing the value of lower by 1.

If you run this program, it will print similar output.

Enter the lower range: 3
Enter the upper range: 10
4
6
8
10

Method 3: By using a while loop and jumping numbers:

Instead of iterating over all the numbers, we can jump between the even numbers. We know that if one number is even, the next number in the number series will be odd and so on. We can start the loop from the first even number and on each iteration, we can increment its value by 2 to point to the next even number.

The following program shows how to write it with a while loop:

lower = int(input("Enter the lower range: "))
upper = int(input("Enter the upper range: "))

if lower % 2 != 0:
    lower = lower + 1

while lower <= upper:
    print(lower)
    lower += 2

Download the code on Github

This is similar to the previous program, but we are changing the value of the lower variable to the first even number before the loop starts. If the user-given lower range is an even value, it will not change it. Else, it increments the lower variable by 1. On each iteration, the value of the lower variable is incremented by 2.

It will print similar output:

Enter the lower range: 4
Enter the upper range: 12
4
6
8
10
12

Method 4: By using a for loop and jumping numbers:

The range() function returns a list of numbers. We can use this function to print the even numbers in a given range. The syntax of this function is:

range(start, stop, step)
  • The start parameter defines the starting position. It is an optional value and it is 0 by default.
  • The stop parameter defines the position to stop.
  • The step parameter defines the steps in between the numbers. It is 1 by default. It is an optional value.

If we provide 2 as the step, it will skip one number on each step in the sequence. To print out all the even numbers, we can point the value of lower to the smallest even value and use it with the range() function as below:

lower = int(input("Enter the lower range: "))
upper = int(input("Enter the upper range: "))

if lower % 2 != 0:
    lower = lower + 1

for i in range(lower, upper + 1, 2):
    print(i)

Sample output:

Enter the lower range: 1
Enter the upper range: 10
2
4
6
8
10

Get the code on Github

Conclusion :

In this example, we have learned how to print all the even numbers in a range in Python in different ways. We can use the same methods to find out all the odd numbers as well.

You might also like :