[LeetCode] Subtract the Product and Sum of Digits of an Integer

1281. Subtract the Product and Sum of Digits of an Integer

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

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int subtractProductAndSum(int n) {
int p = 1, s = 0;
while(n) {
p *= (n % 10);
s += (n % 10);
n /= 10;
}
return p - s;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/26/PS/LeetCode/subtract-the-product-and-sum-of-digits-of-an-integer/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.