classSolution {
public:int subtractProductAndSum(int n) {
int prod =1, s =0;
while (n >0) {
int d = n %10;
prod *= d;
s += d;
n /=10;
}
return prod - s;
}
};
classSolution {
publicintsubtractProductAndSum(int n) {
int prod = 1, s = 0;
while (n > 0) {
int d = n % 10;
prod *= d;
s += d;
n /= 10;
}
return prod - s;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
funsubtractProductAndSum(n: Int): Int {
var num = n
var prod = 1var s = 0while (num > 0) {
val d = num % 10 prod *= d
s += d
num /=10 }
return prod - s
}
}
1
2
3
4
5
6
7
8
9
classSolution:
defsubtractProductAndSum(self, n: int) -> int:
prod, s =1, 0while n >0:
d = n %10 prod *= d
s += d
n //=10return prod - s
1
2
3
4
5
6
7
8
9
10
11
12
13
impl Solution {
pubfnsubtract_product_and_sum(mut n: i32) -> i32 {
letmut prod =1;
letmut s =0;
while n >0 {
let d = n %10;
prod *= d;
s += d;
n /=10;
}
prod - s
}
}