Finding Odd or Even number using if and else

To find odd and even number, we will be using if and else condition. This if and else condition will be created inside a function that we can call after getting value from the keyboard.

We will start by creating a function call oddeven which includes if and else statement. This oddeven function will have one parameter called “num”.

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

To find odd or even number, we have to divide the number by 2. If the remainder is zero, number is even. If not then that number is odd. oddeven function include one parameter “num” which takes the value when the function is call.

The next step is to ask user to input the number from keyboard.

number = int(input("Please enter any number: "))

At last calling the function and passing the number got from keyboard to the parameter “num” defined in the oddeven function.

oddeven(number)

Now inside the oddeven function, if condition will check whether the result of division of num by 2 is 0 or not. If the result is 0 or zero , statement ” Even number” will be printed. If not then printing the statement ” Odd number”.

Output

Please enter any number: 5
 Odd Number

Full Code

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


number = int(input("Please enter any number: "))
oddeven(number)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s