Product of digits#906

Product of digits (Editorial)

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:

Let be a three-digit number, its hundreds digit, its tens digit and its units digit, so that n = 100a + 10b + c. Then:

It remains to compute and print the product a \cdot b \cdot c.

Example

For n = 235:

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 % 10

Compute 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)