Subtract Product and Sum of digits of an integer

mail@pastecode.io avatar
unknown
c_cpp
2 months ago
340 B
0
Indexable
Never
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;
    }
};
Leave a Comment