To find the product of the digits, we first need to extract each digit of the number separately. Two integer arithmetic operations are used for this:
the integer division discards the last digit of the number;
the remainder equals the last digit of the number.
Let be a three-digit number, its hundreds digit, its tens digit and its units digit, so that n = 100a + 10b + c. Then:
the hundreds digit equals : integer division by discards the last two digits;
the tens digit equals : division by discards the units digit, and the tens digit becomes the last one;
the units digit equals .
It remains to compute and print the product a \cdot b \cdot c.
Example
For n = 235:
a = 235 / 100 = 2;
b = 235 / 10~\%~10 = 23~\%~10 = 3;
c = 235~\%~10 = 5.
The product of the digits is 2 \cdot 3 \cdot 5 = 30.
Algorithm implementation
Read the input data.
scanf("%d",&n);Compute the hundreds digit , tens digit , and units digit .
a = n / 100;
b = n / 10 % 10;
c = n % 10;Compute and print the product of the digits.
res = a * b * c;
printf("%d\n",res);Java implementation
import java.util.*;
public class Main
{
public static void main(String []args)
{
Scanner con = new Scanner(System.in);
int n = con.nextInt();
int a = n / 100;
int b = n / 10 % 10;
int c = n % 10;
int res = a * b * c;
System.out.println(res);
con.close();
}
} Python implementation
Read the input data.
n = int(input())Compute the hundreds digit , tens digit , and units digit .
a = n // 100
b = n // 10 % 10
c = n % 10Compute and print the product of the digits.
res = a * b * c
print(res)Python implementation — digits
Read the input data.
n = input()Compute and print the product of the digits.
res = int(n[0]) * int(n[1]) * int(n[2])
print(res)