Even Number Python Program

Python program to check given number is an even number or not? We will discuss different methods to check number is an even number or not. A number will be given to the program and the python program will check the given number is an even number or not. We will also find all even numbers in between a given range in Python.

Even number:- A number is called an even number if it is divisible by 2.
Example:- 4,8,12 e.t.c. are even numbers but 3,5,7,9,11 are not an even number because they are not completely divisible by number 2, they are odd numbers.

Check if the Number is Even in Python

This is the simplest and easiest python program to check given number is an even number or not. We will take one number while declaring the variables. Python program will check given number is an even number or not using mathematical calculation and finally, it will be displayed on the screen.

Program description:- Write a Python program to check number is even?

# Python program to check given number is an even or not

# take inputs
num = 10

# check number is even or not
if(num % 2 == 0):

 print('{0} is an even number'.format(num))
else:
 print('{0} is not an even number'.format(num))

Output:-

10 is an even number

In this program, value for the number was hardcoded in the program.

num = 10

Then, checked the given number is an even number or not using the if-else statement and finally, display the result.

if(num % 2 == 0):
 print('{0} is an even number'.format(num))
else:
 print('{0} is not an even number'.format(num))

Even Number in Python

In the previous program, inputs were hardcoded in the program but in this program, inputs will be provided by the user.

# Python program to check given number is an even or not

# take inputs
num = int(input('Enter a number: '))

# check number is even or not
if(num % 2 == 0):
 print('{0} is an even number'.format(num))
else:
 print('{0} is not an even number'.format(num))

Output for the different input values:-

Enter a number: 4
4 is an even number

Enter a number: 5
5 is not an even number

Enter a number: 628
628 is an even number

In this program, inputs are scanned using the input() function and stored in variable num.

num = int(input('Enter a number: '))

Then, check the given number is an even number or not using the if-else statement and finally, display the result.

Even Number Python Program using Functions

We can also take the help of a function to check number is even or not in python. A function is a block of code that performs a specific task.

# Python program to check given number is an even or not using function

# Returns true if num is even, else not even 
def isEven(num):
 # check number is even or not
 return (num % 2 == 0)

#take inputs
num = int(input('Enter a number: '))

# display result
if isEven(num):
 print(num," is an even number")
else:
 print(num," is not an even number")

Output for the different input values:-

Enter a number: 12
12 is an even number

Enter a number: 15
15 is not an even number

In this program, first of all a function isEven() is defined which checks the passed value is an even number or not?

# Returns true if num is even, else not even 
def isEven(num):
 # check number is even or not
 return (num % 2 == 0)

Inputs are scanned using the input() function and stored in variable num. Then call the function and print the result condition.

Python Program to Print All Even Numbers in a Range

In this program, We will learn how to print even numbers in the given range. Example:- Python program will be print all even numbers in the 1 to 10 number range.

# Python program to print all even numbers in given range 

# take range
start, end = 1, 10

print('All even number in the range')

for num in range(start, end + 1):
    # check number is even or not
    if num % 2 == 0:
        print(num, end = " ")

Output:-

All even number in the range
2 4 6 8 10

In the previous program, ranges are hardcoded in the program but in this program, range will be provided by the user.

# Python program to print all even numbers in given range 

# take range
start = int(input('Start: '))
end = int(input('End: '))

print('All even number in the range')

for num in range(start, end + 1):
    # check number is even or not
    if num % 2 == 0:
        print(num, end = " ")

Output for the different input values:-

Start: 5
End: 25
All even number in the range
6 8 10 12 14 16 18 20 22 24

Start: 1
End: 100
All even number in the range
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100

Also See:- Python program to find area of circle

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *