Python Program to Calculate the Product of Digits of a Number at Even and Odd places

In the previous article, we have discussed Python Program to Check Whether Product of Digits at Even places of a Number is Divisible by K
Given a number, the task is to calculate whether or not the product of its digits at even and odd places.

Examples:

Example1:

Input:

Given Number = 432172

Output:

The product of all digits at even places in a given number{ 432172 } = 56
The product of all digits at odd places in a given number { 432172 } = 6

Example2:

Input:

Given Number = 2134315

Output:

The product of all digits at even places in a given number{ 2134315 } = 90
The product of all digits at odd places in a given number { 2134315 } = 4

Program to Check if Product of Digits of a Number at Even and Odd places is Equal in Python

Below are the ways to determine whether or not the product of its digits at even and odd places is equal.

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_prodt” and initialize it with 1.
  • Take another variable say “od_prodt” and initialize it with 1.
  • Loop in the above list of digits until the length of the “digtslst” using the for loop.
  • Check if the iterator value is even or not using the if conditional statement.
  • If the statement is true, then multiply the iterator value of “digtslst” to the “evn_prodt” and store it in the same variable evn_prodt.
  • If the statement is false, then multiply the iterator value of “digtslst” to the “od_prodt” and store it in the same variable od_prodt.
  • Print “evn_prodt to get the product of all digits at even places in a given number.
  • Print “od_prodt to get the product of all digits at odd places 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 = 432172
# 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_sum" and initialize it with 1.
evn_prodt = 1
# Take another variable say "od_sum" and initialize it with 1.
od_prodt = 1
# 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 is even or not using
    # the if conditional statement.
    if(itr % 2 == 0):
        # If the statement is true, then multiply the iterator value of "digtslst" to the "evn_prodt"
        # and store it in the same variable evn_prodt.
        evn_prodt *= digtslst[itr]
    else:
     # If the statement is false, then add the iterator value of "digtslst" to the "od_prodt"
     # and store it in the same variable od_sum.
        od_prodt *= digtslst[itr]
# Print "evn_prodt" to get the product of all digits at even places in a given number.
print(
    "The product of all digits at even places in a given number{", numb, "} =", evn_prodt)
# Print "od_prodt" to get the product of all digits at odd places in a given number.
print(
    "The product of all digits at odd places in a given number {", numb, "} =", od_prodt)

Output:

The product of all digits at even places in a given number{ 432172 } = 56
The product of all digits at odd places in a given number { 432172 } = 6

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 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_prodt” and initialize it with 1.
  • Take another variable say “od_prodt” and initialize it with 1.
  • Loop in the above list of digits until the length of the “digtslst” using the for loop.
  • Check if the iterator value is even or not using the if conditional statement.
  • If the statement is true, then multiply the iterator value of “digtslst” to the “evn_prodt” and store it in the same variable evn_prodt.
  • If the statement is false, then multiply the iterator value of “digtslst” to the “od_prodt” and store it in the same variable od_prodt.
  • Print “evn_prodt to get the product of all digits at even places in a given number.
  • Print “od_prodt to get the product of all digits at odd places 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_sum" and initialize it with 1.
evn_prodt = 1
# Take another variable say "od_sum" and initialize it with 1.
od_prodt = 1
# 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 is even or not using
    # the if conditional statement.
    if(itr % 2 == 0):
        # If the statement is true, then multiply the iterator value of "digtslst" to the "evn_prodt"
        # and store it in the same variable evn_prodt.
        evn_prodt *= digtslst[itr]
    else:
     # If the statement is false, then add the iterator value of "digtslst" to the "od_prodt"
     # and store it in the same variable od_sum.
        od_prodt *= digtslst[itr]
# Print "evn_prodt" to get the product of all digits at even places in a given number.
print(
    "The product of all digits at even places in a given number{", numb, "} =", evn_prodt)
# Print "od_prodt" to get the product of all digits at odd places in a given number.
print(
    "The product of all digits at odd places in a given number {", numb, "} =", od_prodt)

Output:

Enter some random number = 2134315
The product of all digits at even places in a given number{ 2134315 } = 90
The product of all digits at odd places in a given number { 2134315 } = 4

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.