Program to Check Whether Product of Digits at Even places of a Number is Divisible by K

Python Program to Check Whether Product of Digits at Even places of a Number is Divisible by K

In the previous article, we have discussed Python Program to Check Whether Sum of digits at Odd places of a Number is Divisible by K
The task is to check if the product of digits at even places of a given number is divisible by the another given input number say K.

Examples:

Example1:

Input:

Given  Number =  693214
Given  another Number (k) = 2

Output:

The Product of digits at even places of the given number{ 693214 } is divisible by the another given number k{ 2 }

Example2:

Input:

Given Number =  2578
Given another Number (k) = 4

Output:

The Product of digits at even places of the given number{ 2578 } is not divisible by the another given number k{ 4 }

Program to Check Whether Product of Digits at Even places of a Number is Divisible by K in Python

Below are the ways to check if the product of digits at even places of a given number is divisible by the another given input number say K:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Give the other number k as static input and store it in another 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 another variable say “evn_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 element of digits list at the iterator value to the “evn_prodt” and store it in the same variable evn_prodt.
  • Check if the evn_prodt modulus given number k is equal to 0 or not using the if conditional statement.
  • If the statement is true, then print “The product of digits at even places of the given number is divisible by the another given number k.
  • If the statement is false, then print “The product of digits at even places of the given number is Not divisible by the another given number k.
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 123456
# Give the other number k as static input and store it in another variable.
gvn_k = 5
# Convert the given number to a string using the str() function and store it in
# another variable.
stringnum = str(gvn_numb)
# Create a list of digits say "digtslst" using map(),list(),int functions.
digtslst = list(map(int, stringnum))
# Take another variable say "evn_prodt" and initialize it with 1.
evn_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 element of digits list at iterator value to
        # the "evn_prodt" and store it in the same variable evn_prodt.
        evn_prodt *= digtslst[itr]
# Check if the evn_prodt modulus given number k is equal to 0 or not using the if conditional
# statement.
if(evn_prodt % gvn_k == 0):
    # If the statement is true, then print "The product of digits at even places of the given
    # number is divisible by the another given number k.
    print("The Product of digits at even places of the given number{", gvn_numb,
          "} is divisible by the another given number k{", gvn_k, "}")
else:
    # If the statement is false, then print "The product of digits at even places of the given
    # number is Not divisible by the another given number k.
    print("The Product of digits at even places of the given number{", gvn_numb,
          "} is not divisible by the another given number k{", gvn_k, "}")

Output:

The Product of digits at even places of the given number{ 123456 } is divisible by the another given number k{ 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.
  • Give the other number k as user input using the int(input()) function and store it in another 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 another variable say “evn_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 element of digits list at the iterator value to the “evn_prodt” and store it in the same variable evn_prodt.
  • Check if the evn_prodt modulus given number k is equal to 0 or not using the if conditional statement.
  • If the statement is true, then print “The product of digits at even places of the given number is divisible by the another given number k.
  • If the statement is false, then print “The product of digits at even places of the given number is Not divisible by the another given number k.
  • 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.
gvn_numb = int(input("Enter some random number = "))
#Give the other number k as user input using the int(input()) function and store it in another variable.
gvn_k = int(input("Enter some random number = ")) 
# Convert the given number to a string using the str() function and store it in
# another variable.
stringnum = str(gvn_numb)
# Create a list of digits say "digtslst" using map(),list(),int functions.
digtslst = list(map(int, stringnum))
# Take another variable say "evn_prodt" and initialize it with 1.
evn_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 element of digits list at iterator value to
        # the "evn_prodt" and store it in the same variable evn_prodt.
        evn_prodt *= digtslst[itr]
# Check if the evn_prodt modulus given number k is equal to 0 or not using the if conditional
# statement.
if(evn_prodt % gvn_k == 0):
    # If the statement is true, then print "The product of digits at even places of the given
    # number is divisible by the another given number k.
    print("The Product of digits at even places of the given number{", gvn_numb,
          "} is divisible by the another given number k{", gvn_k, "}")
else:
    # If the statement is false, then print "The product of digits at even places of the given
    # number is Not divisible by the another given number k.
    print("The Product of digits at even places of the given number{", gvn_numb,
          "} is not divisible by the another given number k{", gvn_k, "}")

Output:

Enter some random number = 693214
Enter some random number = 2
The Product of digits at even places of the given number{ 693214 } is divisible by the another given number k{ 2 }

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.