HomePythonPython Program to Check If a Number is Even or Odd

Python Program to Check If a Number is Even or Odd

In this python tutorial, you will learn how to Check If a Number is Even or Odd using the if and else statements along with the modulus operator of the python programming language.

How to Check If a Number is Even or Odd in Python?

Let’s take a look at the first source code , here the values are assigned in the code, the == equality operator and the modulus operator along with the if and else  statements carry out the function.

RUN CODE SNIPPET
num = 12  
  if (num % 2) == 0:     
print("{0} is an Even number".format(num)) 
  else:    
print("{0} is an Odd number".format(num))

OUTPUT:

12 is an Even number
  1. At the start, we declare the variable num and assign the integer value 12.
  2. We declare the if statement with the condition (num % 2) == 0 in which the % operator divides the integer value by 2 and if the value equals 0, the condition is true, if it is not equal to 0, then the condition is false.
  3. Now, we display the output value using the print and display the statement/string ("{0} is an Even number".format(num)) followed by the else statement and the statement/string ("{0} is an Odd number".foramt(num))
  4. In the above statement/string, the variables {0} will hold the value num where the format function helps in variable substitution and data formatting.
  5. As given in the second point, with respect to whether the condition is true or false the respective print statement will be displayed with the help of the if and else statement.

Let’s take a look at the second source code , here the values are given as input by the user in the code, the == equality operator and the modulus operator along with the if and else statements carry out the function.

RUN CODE SNIPPET
num = int(input("Enter a number: "))  
if (num % 2) == 0:  
   print("\n{0} is an Even number".format(num))  
else:  
   print("\n{0} is an Odd number".format(num))

INPUT:

13

OUTPUT:

Enter a number: 
13 is an Odd number
  1. Here we give the user the option to enter the values and the input values are scanned using the input  function and are stored the variables num with the statements/strings Enter a number:.
  2. In the STDIN section of the code editor the input values are entered.
  3. Now, we display the output value using the print and display the statement/string ("{0} is an Even number".format(num)) followed by the else statement and the statement/string ("{0} is an Odd number".foramt(num))
  4. In the above statement/string, the variables {0} will hold the value num where the format function helps in variable substitution and data formatting.
  5. As given in the second point, with respect to whether the condition is true or false the respective print statement will be displayed with the help of the if and else statement.

NOTE:

  • The == equality operator is a comparison operator which returns True is the two items are equal and returns False if not equal.
  • The modulus operator %  is used to compute the reminder.
  • The if and else statements evaluates whether an expression is true or false. If a condition is true, the “if” statement is executed otherwise, the “else” statement is executed.
  • The colon : at the end of the if and else statement tells Python that the next line of code should only be run if the condition is true.
  • The statement for the input function are enclosed in single quotes and parenthesis.
  • The \n in the code indicates a new line or the end of a statement line or a string.
  • The variables used by the format function for substitution is enclosed in curly braces.
  • The print statement is followed by a period, to initiate the format function.
  • The print statement/string to be displayed in enclosed in double quotes.

Leave A Reply

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

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...