Python Even or Odd Program

Python Even or Odd Program
Reading Time: 5 minutes

Introduction:

In Python, determining whether a given number is even or odd is a common task. An even number is divisible by 2 without leaving any remainder, whereas an odd number will have a remainder of 1 when divided by 2. In this program, we will write a Python program to check if a number is even or odd.

Algorithm:

  1. Start the program.
  2. Take an input number from the user and store it in a variable.
  3. Check if the number modulo 2 is equal to 0.
  4. If the condition is true, print “Even number”.
  5. If the condition is false, print “Odd number”.
  6. End the program.

Explanation:

This program prompts the user to enter a number and then determines whether the entered number is even or odd. Here’s a step-by-step explanation of how the program works:

  1. The program starts by displaying the message “Enter a number: ” to the user, indicating that they should input a number. The input() function is used to receive user input from the keyboard.
  2. The user’s input is captured as a string, which is then converted to an integer using the int() This is necessary because the input function returns a string, and we need to perform mathematical operations on the number later on.
  3. The converted number is assigned to the variable number, which will hold the value entered by the user.
  4. The program then checks whether the number is even or odd using the modulo operator %. The modulo operator calculates the remainder of the division between two numbers. In this case, we’re dividing number by 2.
  5. If the remainder of the division (number % 2) is equal to 0, it means that the number is divisible by 2 without any remainder, indicating that it is an even number. In this case, the program executes the print(“Even number”)
  6. If the remainder of the division is not equal to 0, it means that the number is not divisible by 2 without any remainder, indicating that it is an odd number. In this case, the program executes the print(“Odd number”)
  7. After either the “Even number” or “Odd number” message is printed, the program terminates.

Code :

    
     # Prompt the user to enter a number
number = int(input("Enter a number: "))
# Check if the number is even or odd
if number % 2 == 0:
    print("Even number")
else:
    print("Odd number")


    
   

Example 1 :

Input :

    
     Enter the number : 24
    
   

Output :

    
     Even number
    
   

Example 2 :

Input :

    
     Enter the number : 37
    
   

Output :

    
     Odd number
    
   

In the above program, the user is prompted to enter a number. The number is then checked using the modulo operator % to determine if it’s even or odd. If the remainder when dividing the number by 2 is 0, it is considered even and “Even number” is printed. Otherwise, if the remainder is 1, it is considered odd and “Odd number” is printed.