Python if Example Even Odd

By | May 10, 2020

Task: Python if Example Even Odd

Write a program to input a number and check it whether it is even or odd number.

Python if example program to check a number is even or odd

Python if example program to check a number is even or odd

Source Code

#  Write a Python If Example Program
#  Even Odd to input a number and check it whether
#  it is even or odd


num = int(input("Enter a number to check for Even / Odd:"))

if num%2==0:
    print(num, ' is Even')
else:
    print(num, ' is Odd')

Output


Enter a number to check for Even / Odd:12
12  is Even

Output from a second sample run

Enter a number to check for Even / Odd:109
109  is Odd

How Python if Example Program Even Odd Number

  • First of all we use an input() function to get input from the user.
  • The user will input a number.
  • The number is in the numeric string form.
  • We convert it into integer number using the int() function
  • Now we use Python if else statement to check for even or odd.
    % operator is used to take remainder of the division of number by two.
  • if the remainder of this division is zero then the number is even, otherwise the number is odd. the remainder will be 1 if the number entered is odd.

Source code Using a function even()

#  Write a Python If Example Program
#  Even Odd to input a number and check it whether
#  it is even or odd

def  even(num):
    if num%2==0:
        print(num, ' is Even')
    else:
        print(num, ' is Odd')

def main():
    
    num = int(input("Enter a number to check for Even / Odd:"))
    # call the even() function
    even(num)

# call main()

main()
  • First of all we use an input() function to get input from the user.
  • The user will input a number.
  • The number is in the numeric string form.
  • We convert it into integer number using the int() function
    due to call statement, this number is passed to even(num) function
  • Now we use Python if else statement to check for even or odd.
    % operator is used to take remainder of the division of number by two.
  • if the remainder of this division is zero then the number is even, otherwise the number is odd. the remainder will be 1 if the number entered is odd.

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *