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

C++

1class Solution { 2 public: 3 int subtractProductAndSum(int n) { 4 int p = 1; 5 int s = 0; 6 7 while(n>0) 8 { 9 int t = n%10; 10 n = n/10; 11 12 p *= t; 13 s += t; 14 } 15 16 return p - s; 17 } 18};

Ruby

1def subtract_product_and_sum(n) 2 p = 1 3 s = 0 4 5 while n > 0 do 6 t = n % 10 7 n /= 10 8 9 p *= t 10 s += t 11 end 12 13 p - s 14end

JavaScript

1var subtractProductAndSum = function(n) { 2 let p = 1; 3 let s = 0; 4 5 while (n>0) { 6 let t = n%10; 7 n = Math.floor(n / 10); 8 9 p *= t; 10 s += t; 11 } 12 13 return p - s; 14}; 15