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.
- Start the program.
- Take an input number from the user and store it in a variable.
- Check if the number modulo 2 is equal to 0.
- If the condition is true, print “Even number”.
- If the condition is false, print “Odd number”.
- End the program.
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:
- 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.
- 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.
- The converted number is assigned to the variable number, which will hold the value entered by the user.
- 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.
- 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”)
- 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”)
- After either the “Even number” or “Odd number” message is printed, the program terminates.
# 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 :
Enter the number : 24
Example 2 :
Enter the number : 37
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.