Find elements

Python program to check a number is even or odd using function

Python program to check a number is even or odd using function

In this tutorial, we will discuss the Python program to check a number is even or odd using the function

In this program, we are going to learn about how to find the odd or even number from given number using function in the Python language.

 

First, we must understand what is even or odd?

What is Even or Odd

When the number is divided by 2 and the balance becomes zero and the above number is called as the even number – eg 2,4,6,8,10

and on the other sides when it is divided by 2 and balance becomes 1 they are called odd numbers or uneven numbers

 

In my previous post, I have explained the various ways to check whether the  number is even or odd in Python language

However, in this program, we embed the logic  of the check even or odd numbers program in the function

You can embed the logic of the  program to check even or odd numbers using any of the following approaches in the function

 

Once This program received the number it will check the given number either odd or even.

After receiving the input from the user, it is stored in the variable of num and then program divides the value of num by 2 and displays the output

Using modular operator

num=int(input("Enter a number for check odd or even: "))
def find_Evenodd(num):
    
    if(num%2==0):
        print(num," Is an even")
    else:
        print(num," is an odd")

find_Evenodd(num);//function call

When the above code is executed, it produces the following results

Case 1

Enter a number for check odd or even: 349
349 is an odd

Case 2

Enter a number for check odd or even: 234
234 is an even

 

Using Bitwise operator

num=int(input("Enter a number for check odd or even: "))
def find_Evenodd(num)://function definition
    
    if(num&1==1):
        print(num, "is an odd number");
    else:
        print(num, "is an even number");

find_Evenodd(num);//function call

 

When the above code is executed, it produces the following results

Case 1

Enter a number for check odd or even: 678
678 is an even number

Case 2

Enter a number for check odd or even: 765
765 is an odd number

 

Using the division operator

num=int(input("Enter a number for check odd or even: "))
def find_Evenodd(num)://function definition
    number=(num/2)*2
    if(number==num):
        print(num, "is a even number");
    else:
        print(num, "is a odd number");

find_Evenodd(num);//call the function

When the above code is executed, it produces the following results

Case 1

Enter a number for check odd or even: 678
678 is a even number

Case 2

Enter a number for check odd or even: 987
987 is a odd number

 

Suggested for you

Python operator

python if else statements

Python function

 

Similar post

Python program to check whether a number odd or even

Python program to display even and odd number in the given range

Python code to display all even and odd numbers from 1 to n

Python code to display all even and odd numbers without if

 

Cpp program to check a number is even or odd using function
C function to check a number is even or odd
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

View Comments

  • Write a function named collatz() that has one parameter named number. If number is even, then collatz() should print number // 2 and return this value. If number is odd, then collatz() should print and return 3 * number + 1.

    Then write a program that lets the user type in an integer and that keeps calling collatz() on that number until the function returns the value 1. (Amazingly enough, this sequence actually works for any integer—sooner or later, using this sequence, you’ll arrive at 1! Even mathematicians aren’t sure why. Your program is exploring what’s called the Collatz sequence, sometimes called “the simplest impossible math problem.”)

    Remember to convert the return value from input() to an integer with the int() function; otherwise, it will be a string value.

    Hint: An integer number is even if number % 2 == 0, and it’s odd if number % 2 == 1.

    The output of this program could look something like this:

    Enter number:
    3
    10
    5
    16
    8
    4
    2

  • print('Enter a number = ')
    x=input()
    print('entered number is=',x)
    n = x % 2
    k = 0
    if n > k :
    print(' Even ')
    else :
    print('Odd')

  • num = int(input('enter any number'))
    if num % 2:
    print('number is odd')
    else:
    print('number is even')

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago