Check Divisibility by Digit Sum and Product

You are given a positive integer `n`. Determine whether `n` is divisible by the **sum** of the following two values: - The **digit sum** of `n` (the sum of its digits). - The **digit product** of `n` (the product of its digits). Return `true` if `n` is divisible by this sum; otherwise, return `false`.

30:00

Check Divisibility by Digit Sum and Product
easy
Free written solution
Topics
Companies

You are given a positive integer n. Determine whether n is divisible by the sum of the following two values:

  • The digit sum of n (the sum of its digits).
  • The digit product of n (the product of its digits).

Return true if n is divisible by this sum; otherwise, return false.

Example 1:
Input: {"n":99}
Output: true
Constraints:
  • 1≤n≤1061 \leq n \leq 10^6

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":99}

Start the digit sum at s = 0 and the digit product at p = 1 (the identities for + and x), then peel digits off 99.

n = 99

9
9

digit sum s

0

= 0

digit product p

1

= 1

n % (s + p) == 0 ?  waiting for every digit

Variables
VariableValue
n99
s0
p1
DepthFunction Call
Stack empty
0/7