LeetCode_Study_Plan/Programming Skills

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

개발하는루루 2022. 9. 19. 21:05

https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/

 

Subtract the Product and Sum of Digits of an Integer - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

class Solution {
    public int subtractProductAndSum(int n) {
        int producted = 1;
        int added = 0 ;
        int tmp_num = -1;

        while (n != 0) {
            tmp_num = n % 10;
            n /= 10;
            producted *= tmp_num;
            added += tmp_num;
        }

        return producted - added;
    }
}