Subtract Product and Sum of digits of an integer
unknown
c_cpp
2 years ago
340 B
14
Indexable
class Solution {
public:
int subtractProductAndSum(int n) {
int product = 1;
int sum = 0;
while(n!=0){
int digit = n%10;
product *= digit;
sum += digit;
n = n/10;
}
int ans = product - sum;
return ans;
}
};Editor is loading...
Leave a Comment