/*
Given an integer number n, return the difference between the product of its digits and the sum of its digit
Example 1:
Input: n = 234
Output: 15
Explanation:
Product of digits = 2 * 3 * 4 = 24
Sum of digits = 2 + 3 + 4 = 9
Result = 24 - 9 = 15
*/
public class Solution
{
public static int subtractProductAndSum(int num) {
int product = 1;
int sum = 0;
int digit;
if (num <=0 ) return 0;
while (num > 0) {
digit = num % 10 ;
product*= digit;
sum+= digit;
num = num / 10;
}
return (product - sum);
}
public static void main(String[] args) {
System.out.println ("Difference between product and difference : " + subtractProductAndSum(234));
}
}
July 1, 2019
Subtract the Product and Sum of Digits of an Integer
Posted by Khushi under LeetCode | Tags: LeetCode |Leave a Comment
Leave a comment