Problem · Math

Product Minus Sum of Digits

Learn this problem
EasyTiktok logoTiktokINTERNOA
See Tiktok hiring insights

Problem statement

Given a positive integer n, calculate the difference between the product of its digits and the sum of its digits.

The order matters: return product - sum, not sum - product.

Function

subtractProductAndSum(n: int) → int

Examples

Example 1

n = 123456return = 699

The product of the digits is 1 * 2 * 3 * 4 * 5 * 6 = 720, and their sum is 1 + 2 + 3 + 4 + 5 + 6 = 21. Therefore, the result is 720 - 21 = 699.

Example 2

n = 1010return = -2

The product of the digits is 1 * 0 * 1 * 0 = 0, and their sum is 1 + 0 + 1 + 0 = 2. Therefore, the result is 0 - 2 = -2.

Constraints

  • n is a positive integer.

More Tiktok problems

drafts saved locally
public int subtractProductAndSum(int n) {
    // Write your code here.
}
n123456
expected699
checking account