Table of Contents
Python program to check whether a number is even or odd|4 ways
In this tutorial, we will discuss the Python program to check whether a number is even or odd
In this post, we are going to learn how to check whether the given number is even or odd in Python language
Code to check whether a number is even or odd -using standard method
Program 1
num=345 if(num%2)==0: print("{0} is even".format(num)) else: print("{0} is odd".format(num))
When the above code is executed, it produces the following result
345 is odd
In this program,
- integer variables num is declared and initialized
- The given number is tested whether it is odd or even
- Then,the program is displayed the output of using print() function.
Code to check whether a number is even or odd -using user input
Program 2
num=int(input("Enter a number: ")) if(num%2)==0: print("{0} is even".format(num)) else: print("{0} is odd".format(num))
When the above code is executed, it produces the following result
case 1
Enter a number: 4567 4567 is odd
case 2
Enter a number: 486 486 is even
Approach
- integer variable num is declared.
- The program asks input from the user
- Then the user enters the input values for num.
- The program will read the input using input stream and store to the variable num
- The given number is tested whether it is odd or even.
- Then,the program is displayed the output of using printf() function,
Code to check whether a number is even or odd -using function
Program 3
#Python function to check Even or Odd num=int(input("Enter a number: ")) def checkOddEven(num): if (num%2==0): print(num," is Even") else: print(num," is Odd") checkOddEven(num) #call the function
When the above code is executed, it produces the following result
case 1
Enter a number: 179 179 is Odd
Case 2
Enter a number: 210 210 is Even
Approach
- integer variable num is declared.
- The program takes input from the user
- Then the user enters the input values for num.
- The program will read the input using input stream and store to the variable num
- Define the function to check whether number is odd or even.
- calling the function
- Finally the program is displayed the output using print() function.
Code to check whether a number is even or odd -using recursion
Program 4
#Code to check whether a number is even or odd -using recursion def isEven(num):#function definition if(num>2): return (num%2==0) return(isEven(num-2)) num=int(input("Enter a number to check odd or even: ")) #receive input from the user and stores variabe num if(isEven(num)==True): # number is passed as argument to the function print(num," is an even") else: print(num," is an odd")
When the above code is executed, it produces the following result
Case 1
Enter a number to check odd or even: 657 (657, ' is an odd')
Case 2
Enter a number to check odd or even: 7654 (7654, ' is an even')
Similar post
Python program to check whether a number is even or odd
Java program to check whether a number is even or odd
C program to check whether a number is even or odd
C++ program to check whether a number is even or odd
Suggested post