Python program to check given number is even or odd.



Program to check given number is even or odd.

In this post we will see a program to check given number is even or odd. Let's start..!

To write this program first understand How can we identify given number is even or odd ?


You have already studied in mathematics what is even number and what is odd number. So you know all even number are divisible by 2. It means if you divide any number with zero( 0 ) and if remainder is 0 then that number will be even. Or if you divide any even number with 2 the remainder will be always zero ( 0 ). 

Now we know that how we can identify a given number is even or odd.

Let's see algorithm of this program. but What is Algorithm ? 


Algorithm : 

Algorithm means a simply step by step solution of any problem or example. We write a step by step solution to solve a problem that steps known as Algorithm.


Algorithm :

step 01: Start

step 02: Create a variable 'number' 

step 03: Print a message "Enter a number :: "

step 04: Take a input from user and store it in variable 'number'

step 05: Check a condition ( number % 2 == 0 ). If condition is true print "Even number" Or if condition is false print "Odd number" 

step 06: Stop


Explanation :

The step 01 simply says start your algorithm or solution.

The step 02 simply says that create a variable named as number ( here your variable name can be anything not mandatory to have you variable name same as I shown here ) . this variable will store a value entered by user.

[ if you don't know how to create variables then click here to learn more about Variables in Python ]

The step 03 says that simply print a  message "Enter a number :: " to tell a user to enter a number that user want to check even or odd.

The step 04 says simply take a input from user and store that number in variable number. 

[ if you don't know how to take input from user then click here to learn more about input( ) in Python ]

The step 05 says that check a condition using if else to check the entered number is even or odd. And if condition is true then print a message "Even number" ( here you can print any text that you want. ). Or if condition is false then print a message "Odd number" ( here you can print any text that you want. ). 

[ if you don't know how to use if else then click here to learn more about if else in Python ]

The step 06 says that your problem has solved so stop.

Now write a Program according to this algorithm. 


Program : 

# start

# created a variable
number = 0
# printing a message to enter a number
print("Enter a number :: ")
# taking input from user 
# integer numbers only so use int()
number = int(input())

# checking condition 
# using if else
if number % 2 == 0 :
    # condition is true
    # print message that number is Even
    print("Even number !")
else:
    # condition is false
    # print message that number is Odd
    print("Odd number !")

# stop 


Output : 

sample input and output 1


Enter a number ::
10
Even number !




sample input and output 2


Enter a number ::
7
Odd number !




If you have any doubts in this post then please ask me in comment section below. I will try to solve it.


If you like these post and you have learned something from this then share this post with your friends and family.

Keep learning always.....!!! 😇😇😇




Previous Post  Next Post