Signup/Sign In
PUBLISHED ON: JULY 1, 2021

Python program to print all even numbers in a range

In this tutorial, we will learn to write a program that will print all the even numbers in a range. Even numbers are the numbers that are divisible by 2. The user has to input the upper limit and the lower limit of the range. Then we have to find all the even numbers in that range and display them. We will be using the concept of loops in Python and conditional statement in Python in our program.

For a given list of numbers, the task is to find and print all the even numbers in that range.

Input: lower limit= 4

upper limit= 10

Output: 4 6 8

Input: lower limit= 7

upper limit= 37

Output: 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36

Approach to print all even numbers in a range

For executing this task, we will use a for loop which will run from the lower limit to the upper limit. We will check all the numbers in that range if they are divisible by 2 or not. Numbers that satisfy the condition will be printed.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Take input of lower limit of the range

Step 2- Take input of upper limit of the range

Step 3- Run a loop from the lower limit to the upper limit

Step 4- Check for each number in the range is divisible by 2 or not

Step 5- If yes, print the number

Python Program 1

Look at the program to understand the implementation of the above-mentioned approach.

#print even numbers
#in range

ll=int(input("Enter lower limit "))
ul=int(input("Enter upper limit "))

print("Even numbers in the range are")

# loop

for i in range(ll,ul):
    if i%2==0:
        print(i,end=" ")


Enter lower limit 6
Enter upper limit 20
Even numbers in the range are
6 8 10 12 14 16 18

For including both the limits in the range we will run the loop from the lower limit to (upper limit+1)

To print the numbers with space we have used end=" "

Conclusion

In this tutorial, we have learned how to find and print all the even numbers in a range. We have used for loop and if conditional statement for checking and finding all the even numbers in the range.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.