Program to Count the Number of Odd and Even Digits

Python Program to Count the Number of Odd and Even Digits

In the previous article, we have discussed Python Program to Replace a Word with Asterisks in a Sentence
Given a number and the task is to count the number of odd and even digits in a given number.

Examples:

Example1:

Input:

Given Number = 1237891

Output:

The count of even digits in a given number{ 1237891 } = 2
The count of odd digits in a given number{ 1237891 } = 5

Example2:

Input:

Given Number = 78342186453

Output:

The count of even digits in a given number{ 78342186453 } = 6
The count of odd digits in a given number{ 78342186453 } = 5

Program to Count the Number of Odd and Even Digits in Python

Below are the ways to count the number of odd and even digits in a given number :

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Convert the given number to a string using the str() function and store it in another variable.
  • Create a list of digits say “digtslst” using map(),list(),int functions.
  • Store it in another variable.
  • Take a variable say “evn_count” and initialize it with 0.
  • Take another variable say “od_count” and initialize it with 0.
  • Loop in the above list of digits until the length of the “digtslst” using the for loop.
  • Check if the iterator value of “digtslst” is even or not using the if conditional statement.
  • If the statement is true, increment the count value of “evn_count” by 1.
  • If the statement is false, increment the count value of “od_count” by 1.
  • Print “evn_count” to get the count of even digits in a given number.
  • Print “od_count to get the count of odd digits in a given number.
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
numb = 1237891
# Convert the given number to string using the str() function.
stringnum = str(numb)
# Create a list of digits say "digtslst" using map(),list(),int functions.
digtslst = list(map(int, stringnum))
# Take a variable say "evn_count" and initialize it with 0.
evn_count = 0
# Take another variable say "od_count" and initialize it with 0.
od_count = 0
# Loop in the above list of digits until the length of the "digtslst" using the for loop.
for itr in range(len(digtslst)):
    # Check if the iterator value of "digtslst" is even or not using
    # the if conditional statement.
    if(digtslst[itr] % 2 == 0):
        # If the statement is true, increment the count value of "evn_count" by 1.
        evn_count += 1
    else:
        # If the statement is false, increment the count value of "od_count" by 1.
        od_count += 1
# Print "evn_count" to get the count of even digits in a given number.
print("The count of even digits in a given number{", numb, "} =", evn_count)
# Print "od_count" to get the count of odd digits in a given number.
print("The count of odd digits in a given number{", numb, "} =", od_count)

Output:

The count of even digits in a given number{ 1237891 } = 2
The count of odd digits in a given number{ 1237891 } = 5

Method #2: Using For loop (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Convert the given number to string using the str() function.
  • Create a list of digits say “digtslst” using map(),list(),int functions.
  • Take a variable say “evn_count” and initialize it with 0.
  • Take another variable say “od_count” and initialize it with 0.
  • Loop in the above list of digits until the length of the “digtslst” using the for loop.
  • Check if the iterator value of “digtslst” is even or not using the if conditional statement.
  • If the statement is true, increment the count value of “evn_count” by 1.
  • If the statement is false, increment the count value of “od_count” by 1.
  • Print “evn_count” to get the count of even digits in a given number.
  • Print “od_count to get the count of odd digits in a given number.
  • The Exit of the Program.

Below is the implementation:

# Give the number as user input using the int(input()) function and
# store it in a variable.
numb = int(input("Enter some random number = "))
# Convert the given number to string using the str() function.
stringnum = str(numb)
# Create a list of digits say "digtslst" using map(),list(),int functions.
digtslst = list(map(int, stringnum))
# Take a variable say "evn_count" and initialize it with 0.
evn_count = 0
# Take another variable say "od_count" and initialize it with 0.
od_count = 0
# Loop in the above list of digits until the length of the "digtslst" using the for loop.
for itr in range(len(digtslst)):
    # Check if the iterator value of "digtslst" is even or not using
    # the if conditional statement.
    if(digtslst[itr] % 2 == 0):
        # If the statement is true, increment the count value of "evn_count" by 1.
        evn_count += 1
    else:
        # If the statement is false, increment the count value of "od_count" by 1.
        od_count += 1
# Print "evn_count" to get the count of even digits in a given number.
print("The count of even digits in a given number{", numb, "} =", evn_count)
# Print "od_count" to get the count of odd digits in a given number.
print("The count of odd digits in a given number{", numb, "} =", od_count)

Output:

Enter some random number = 78342186453
The count of even digits in a given number{ 78342186453 } = 6
The count of odd digits in a given number{ 78342186453 } = 5

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.