Python

Python program to check whether a number is even or odd|4 ways

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

Function in Python language

Operator in Python language

Data type in Python

 

Karmehavannan

Share
Published by
Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.