Find odd digits in an integer value in Python

The below explanation make you understand to how to find odd digits in an integer value in Python.

For example, if the input number is 12345, the output would be the digits 1, 3, and 5 because they are the odd ones in the number

n=int(input("Enter an integer: "))
print("The odd digits in the number are: ")
for digit in str(n):
    if int(digit)%2!=0:
        print(digit)

Breakdown of the code:

  • The code takes an integer input from user. This input() function is to read the user input and the default data type of the user’s input will be string .
  • We then convert the string input to an integer using int() which we do by converting the integer to a string by str(n), to check each digit of the integer. This means we can go through each character (digit) of a string number
  • Inside the loop, each character (digit) is converted back to an integer using int(digit).
  • We then use the modulo operator % to check if the digit is odd. A digit is  odd if digit % 2! = 0.

The int() and str() are the functions that we have used to convert them to integer and string seamlessly  in python. But whereas in other programming languages we should have used complicated ways to convert the data type.

This blog has shown us how to find all odd numbers in a given integer value. Python allows for brief and effective codes that handle intricate problems through its built-in functions as well as loops.

This is how we Find odd digits in an integer value in Python.

Example:

Input:
Enter an integer: 9876543
Output:
The odd digits in the number are: 
9
7
5
3

Leave a Reply

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