How do you print if a number is odd or even in python?

A number is even if it is perfectly divisible by 2. When the number is divided by 2, we use the remainder operator % to compute the remainder. If the remainder is not zero, the number is odd.

Source Code

# Python program to check if the input number is odd or even.
# A number is even if division by 2 gives a remainder of 0.
# If the remainder is 1, it is an odd number.

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

Output 1

Enter a number: 43
43 is Odd

Output 2

Enter a number: 18
18 is Even

In this program, we ask the user for the input and check if the number is odd or even. Please note that { } is a replacement field for num.

Python Program to Check if a Number is Odd or Even

Odd and Even numbers:

If you divide a number by 2 and it gives a remainder of 0 then it is known as even number, otherwise an odd number.

Even number examples: 2, 4, 6, 8, 10, etc.

Odd number examples:1, 3, 5, 7, 9 etc.

See this example:

Output:

How do you print if a number is odd or even in python?


How do you print if a number is odd or even in python?
For Videos Join Our Youtube Channel: Join Now


Feedback

  • Send your Feedback to [email protected]

Help Others, Please Share

How do you print if a number is odd or even in python?
How do you print if a number is odd or even in python?
How do you print if a number is odd or even in python?





Last update on August 19 2022 21:51:46 (UTC/GMT +8 hours)

Python Basic: Exercise-21 with Solution

Write a Python program to find whether a given number (accept from the user) is even or odd, print out an appropriate message to the user.

Pictorial Presentation of Even Numbers:

How do you print if a number is odd or even in python?

Pictorial Presentation of Odd Numbers:

How do you print if a number is odd or even in python?

Sample Solution:-

Python Code:

num = int(input("Enter a number: "))
mod = num % 2
if mod > 0:
    print("This is an odd number.")
else:
    print("This is an even number.")	

Sample Output:

Enter a number: 5                                                                                             
This is an odd number. 

Even Numbers between 1 to 100:

How do you print if a number is odd or even in python?

Odd Numbers between 1 to 100:

How do you print if a number is odd or even in python?

Flowchart:

How do you print if a number is odd or even in python?

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to get a string which is n (non-negative integer) copies of a given string.
Next: Write a Python program to count the number 4 in a given list.

Python: Tips of the Day

Convert a dict to XML:

from xml.etree.ElementTree import Elementdef dict_to_xml(tag, d):
    '''
    Turn a simple dict of key/value pairs into XML
    '''
    elem = Element(tag)
    for key, val in d.items():
        child = Element(key)
        child.text = str(val)
        elem.append(child)
    return elem

I'm trying to make a program which checks if a word is a palindrome and I've gotten so far and it works with words that have an even amount of numbers. I know how to make it do something if the amount of letters is odd but I just don't know how to find out if a number is odd. Is there any simple way to find if a number is odd or even?

Just for reference, this is my code:

a = 0

while a == 0:
    print("\n \n" * 100)
    print("Please enter a word to check if it is a palindrome: ")
    word = input("?: ")

    wordLength = int(len(word))
    finalWordLength = int(wordLength / 2)
    firstHalf = word[:finalWordLength]
    secondHalf = word[finalWordLength + 1:]
    secondHalf = secondHalf[::-1]
    print(firstHalf)
    print(secondHalf)

    if firstHalf == secondHalf:
        print("This is a palindrom")
    else:
        print("This is not a palindrom")


    print("Press enter to restart")
    input()

How do you print if a number is odd or even in python?

mkrieger1

15.6k4 gold badges45 silver badges57 bronze badges

asked Feb 17, 2014 at 19:03

1

if num % 2 == 0:
    pass # Even 
else:
    pass # Odd

The % sign is like division only it checks for the remainder, so if the number divided by 2 has a remainder of 0 it's even otherwise odd.

Or reverse them for a little speed improvement, since any number above 0 is also considered "True" you can skip needing to do any equality check:

if num % 2:
    pass # Odd
else:
    pass # Even 

answered Feb 17, 2014 at 19:05

DeadChexDeadChex

4,2531 gold badge25 silver badges33 bronze badges

5

Similarly to other languages, the fastest "modulo 2" (odd/even) operation is done using the bitwise and operator:

if x & 1:
    return 'odd'
else:
    return 'even'

Using Bitwise AND operator

  • The idea is to check whether the last bit of the number is set or not. If last bit is set then the number is odd, otherwise even.
  • If a number is odd & (bitwise AND) of the Number by 1 will be 1, because the last bit would already be set. Otherwise it will give 0 as output.

How do you print if a number is odd or even in python?

answered Feb 17, 2014 at 19:07

How do you print if a number is odd or even in python?

lejlotlejlot

62.7k8 gold badges128 silver badges158 bronze badges

6

It shouldn't matter if the word has an even or odd amount fo letters:

def is_palindrome(word):
    if word == word[::-1]:
        return True
    else:
        return False

answered Feb 17, 2014 at 19:05

How do you print if a number is odd or even in python?

kylieCattkylieCatt

10.3k5 gold badges40 silver badges51 bronze badges

3

One of the simplest ways is to use de modulus operator %. If n % 2 == 0, then your number is even.

Hope it helps,

answered Feb 17, 2014 at 19:05

Esteban AlivertiEsteban Aliverti

6,0792 gold badges18 silver badges31 bronze badges

Use the modulo operator:

if wordLength % 2 == 0:
    print "wordLength is even"
else:
    print "wordLength is odd"

For your problem, the simplest is to check if the word is equal to its reversed brother. You can do that with word[::-1], which create the list from word by taking every character from the end to the start:

def is_palindrome(word):
    return word == word[::-1]

answered Feb 17, 2014 at 19:05

How do you print if a number is odd or even in python?

Maxime LorantMaxime Lorant

32.5k18 gold badges84 silver badges96 bronze badges

0

The middle letter of an odd-length word is irrelevant in determining whether the word is a palindrome. Just ignore it.

Hint: all you need is a slight tweak to the following line to make this work for all word lengths:

secondHalf = word[finalWordLength + 1:]

P.S. If you insist on handling the two cases separately, if len(word) % 2: ... would tell you that the word has an odd number of characters.

answered Feb 17, 2014 at 19:05

NPENPE

470k103 gold badges919 silver badges994 bronze badges

Not the answer you're looking for? Browse other questions tagged python or ask your own question.