EASY C++ SOLUTION
\n\n# Approach\nInitialization: Initialize two variables, sum and product, both initially set to 0 and 1 respectively. These variables will be used to accumulat
\n\n# Approach\nInitialization: Initialize two variables, sum and product, both initially set to 0 and 1 respectively. These variables will be used to accumulate the sum and product of the digits.\n\nDigit Extraction and Calculation: Enter a loop that continues as long as n is greater than 0. In each iteration of the loop:\n\nCalculate the last digit of n by using the modulo operator (n % 10). This gives you the rightmost digit of n.\nUpdate n by removing its last digit (rightmost digit) using integer division (n / 10). This effectively shifts the digits of n to the right.\nAdd the extracted digit to the sum variable.\nMultiply the extracted digit with the product variable.\nCalculation of Result: After the loop ends, you will have calculated the sum of the digits and the product of the digits of the original number n.\n\nReturn the Difference: Calculate the difference between the product and sum variables and return this difference as the final result.\n\nHere\'s how the code works with an example:\nSuppose n = 123.\n\nIteration 1: a = 3, n = 12, sum = 3, product = 3\nIteration 2: a = 2, n = 1, sum = 5, product = 6\nIteration 3: a = 1, n = 0, sum = 6, product = 6\nThe final result is 6 - 6 = 0.\nIn the end, the function returns 0.\n\nThis approach efficiently extracts the digits of the number and accumulates both the sum and the product in a single loop, making it a straightforward solution to the given problem.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:0(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:0(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int subtractProductAndSum(int n) {\n        int sum =0;\n        int product=1;\n        while(n>0)\n        {\n            int a = n % 10;\n            n = n / 10;\n            sum+=a;\n            product = product*a;\n        }\n        return product-sum;\n    }\n};\n```
['Math', 'C++']