Python Program to Find Product of Mid Digits

This article deals with a program in Python that find and prints product of mid (middle) digits of a number entered by user at run-time. For example, if user enters 2351 as a number, then program prints the product of mid digits of 2351, that will be 3*5 or 15.

Find Product of Mid Digits of Given Number

This program find and prints product of mid digits of a given 4-digit number. Later on, we've modified this program to find the product of mid digits of any digit number entered by user.

print("Enter a 4-digit Number: ")
num = int(input())
t = num
numLen = 0

while t>0:
  numLen = numLen+1
  t = int(t/10)
chk = 0
if numLen==4:
  while num>0:
    rem = num%10
    if chk==1:
      midOne = rem
    elif chk==2:
      midTwo = rem
    num = int(num/10)
    chk = chk+1
  prod = midOne*midTwo
  print("\nProduct of Mid digits: ", prod)
else:
  print("\nIt's not a 4-digit number!")

The snapshot given below shows the initial output produced by this Python program:

python find product of mid digits of number

Now enter a four-digit number say 1243 and press ENTER key to find and print product of mid digits of given number as shown in the snapshot given below:

find product of mid digits python

Modified Version of Previous Program

This program is created after modifying the previous program. This program find and prints product of mid digits of any digit number given by user.

print("Enter the Number: ", end="")
num = int(input())

t = num
numLen = 0
while t>0:
  numLen = numLen+1
  t = int(t/10)

if numLen>=4:
  numLen = int(numLen/2)
  chk = 0
  while num>0:
    rem = num%10
    if chk==numLen:
      midOne = rem
    elif chk==(numLen-1):
      midTwo = rem
    num = int(num/10)
    chk = chk+1
  prod = midOne*midTwo
  print("\nProduct of Mid digits (" +str(midOne)+ "*" +str(midTwo)+ ") = ", prod)
else:
  print("\nIt's not a 4 or more than 4-digit number!")

Here is its sample run with user input 13423:

python find product of mid digits

Here is another sample run with user input 120345:

print product of mid digits python

Python Online Test


« Previous Program Next Program »


Liked this post? Share it!