Subtract the Product and Sum of Digits of an Integer - Problem

Given an integer number n, return the difference between the product of its digits and the sum of its digits.

The product is calculated by multiplying all digits together, and the sum is calculated by adding all digits together.

For example, if n = 234, the product is 2 × 3 × 4 = 24 and the sum is 2 + 3 + 4 = 9, so the answer is 24 - 9 = 15.

Input & Output

Example 1 — Basic Three Digit Number
$ Input: n = 234
Output: 15
💡 Note: Product of digits: 2 × 3 × 4 = 24. Sum of digits: 2 + 3 + 4 = 9. Difference: 24 - 9 = 15
Example 2 — Single Digit Number
$ Input: n = 4
Output: 0
💡 Note: Product of digits: 4. Sum of digits: 4. Difference: 4 - 4 = 0
Example 3 — Number with Zero
$ Input: n = 4021
Output: -7
💡 Note: Product of digits: 4 × 0 × 2 × 1 = 0. Sum of digits: 4 + 0 + 2 + 1 = 7. Difference: 0 - 7 = -7

Constraints

  • 1 ≤ n ≤ 105

Visualization

Tap to expand
Subtract Product and Sum of DigitsInput234Integer nProduct: 2×3×4 = 24Sum: 2+3+4 = 9Output1524 - 9Extract each digit: 4, 3, 2Calculate product and sum simultaneously
Understanding the Visualization
1
Input
Integer n with multiple digits
2
Process
Calculate product and sum of all digits
3
Output
Return product - sum
Key Takeaway
🎯 Key Insight: Use modulo operator to extract digits and process both operations in a single pass
Asked in
Amazon 12 Microsoft 8
25.0K Views
Medium Frequency
~5 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen