Python

Python Program to Check if a Number is Odd or Even2 min read

In this program, we will find whether a given number is even or odd. We have discussed two methods of finding it.

So, firstly we take a number from the user, using the built-in input() function and typecast the return value from input() function into int datatype.




We have used the modulus operator(%) which will give the remainder after dividing the number with 2. And then using the if- else statement we check whether the number is even or odd.

Write a program to Check if a Number is Odd or Even in Python

Code:

The code you provided is a simple Python program that checks whether a number entered by the user is odd or even.

The program starts by using the input() function to prompt the user to enter a number. The user’s input is stored as a string, so it needs to be converted to an integer using the int() function. The variable num is then assigned the value input by the user.

Then, the program uses an if-else statement, which is a control flow statement that allows to check a condition and execute different code based on whether the condition is true or false.

The if statement checks if the remainder of dividing num by 2 is equal to 0. If this condition is true, the program will execute the code within the if block and print “{num} is Even”.

If the condition in the if statement is false, the program will execute the code within the else block and print “{num} is Odd”.

The format() method is used to insert the values of num into the string that is passed to the print() function.

The output will depend on the input provided by the user. For example, if the user inputs 8, the output will be “8 is Even” and if the user inputs 7, the output will be “7 is Odd”.

Leave a Comment