Answers for "1281. Subtract the Product and Sum of Digits of an Integer leetcode solution in c++"

C++
0

1281. Subtract the Product and Sum of Digits of an Integer leetcode solution in c++

#include<iostream>
using namespace std;

class Solution {
public:
	int subtractProductAndSum(int n) {
		int product = 1;
		int sum = 0;
		while (n != 0)
		{
			product *= (n % 10);
			sum += (n % 10);
			n /= 10;
		}
		return product - sum;
	}
};

int main()
{
	int n;
	cin >> n;
	Solution ss;
	cout << ss.subtractProductAndSum(n) << "\n";
	return 0;
}
Posted by: Guest on March-02-2022

Code answers related to "1281. Subtract the Product and Sum of Digits of an Integer leetcode solution in c++"

Browse Popular Code Answers by Language