Coding Tutorials Hub

Learn coding with simple code, clear explanations, and examples

Python program to check whether a given number is Odd or Even using if-else

📄 Code

number = input("Enter a number:")
number = int(number)

if number % 2  == 0:
    print("Even Number")
else:
    print("Odd Number")
        
💡 Output
Enter a number:10
Even Number

Enter a number:1
Odd Number
        
🧠 Code Explanation
Even numbers are numbers that are divisible by 2 which means, the remainder is 0.
Odd numbers are numbers that are not divisible by 2 which means, the remainder is non-zero.

number = input("Enter a number:")

Here, the input function is used to get the input from the user at runtime. 
For example: if the user input is 10, it will be assigned to the variable 'number' as str(string) object.
number  = "10"

number = int(number)
The number "10" which is stored as string is now get converted into integer using int() function and is assigned to the variable 'number'.
number = 10

if number % 2  == 0:
    print("Even Number")
else:
    print("Odd Number")

The body of the 'if' statament will get executed only if the condition provided is True, otherwise, body of 'else' will get executed.

if number % 2  == 0:
    print("Even Number")

if number % 2 == 0:
    => 	if 10 % 2 == 0:
    =>  if 0 == 0: which is True, therefore, the body of the if condition will get executed. ie. 'print("Even Number")' will get executed and the output will be 'Even Number'. 
    
    
Suppose, if the user input is '1',

if number % 2 == 0:
    => 	if 1 % 2 == 0:
    =>  if 1 == 0: which is False, therefore, the body of the 'else' will get executed and not the body of 'if'. ie. 'print("Odd Number")' will get executed and the output will be 'Odd Number'.