#1281

Subtract the Product and Sum of Digits of an Integer

Easy Digit extraction

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Return (product of the digits) minus (sum of the digits) of a positive integer.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: multiply the digits, add the digits, and subtract.

Why this works: % 10 reads the last digit and / 10 removes it. Product starts at 1, sum at 0.

Example: n = 234

digit 4: product 4,  sum 4digit 3: product 12, sum 7digit 2: product 24, sum 924 - 9 = 15

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Starting the product at 0, which makes it 0 forever.