

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Subtract the Product and Sum of Digits of an Integer in C++
Suppose we have one number. We have to find the sum of digits and product of digits. After that find the difference between sum and product. So if the number is 5362, then sum is 5 + 3 + 6 + 2 = 16, and 5 * 3 * 6 * 2 = 180. So 180 – 16 = 164
To solve this take each digit and add and multiply one by one, then return the difference.
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: int subtractProductAndSum(int n) { int prod = 1; int sum = 0; for(int t = n;t;t/=10){ sum += t % 10; prod *= t % 10; } return prod - sum; } }; main(){ Solution ob; cout << ob.subtractProductAndSum(5362); }
Input
5362
Output
164
- Related Questions & Answers
- Python program to find the sum of all even and odd digits of an integer list
- Difference between product and sum of digits of a number in JavaScript
- Product sum difference of digits of a number in JavaScript
- Maximum sum and product of the M consecutive digits in a number in C++
- C++ program to Zoom digits of an integer
- Difference between sum and product of an array in JavaScript
- Java Program to Count Number of Digits in an Integer
- Maximum of sum and product of digits until number is reduced to a single digit in C++
- Find the Largest number with given number of digits and sum of digits in C++
- Recursive product of summed digits JavaScript
- Largest product of contiguous digits in Python
- Finding product of Number digits in JavaScript
- Calculating the sum of digits of factorial JavaScript
- Count even and odd digits in an Integer in C++
- Find smallest number with given number of digits and sum of digits in C++
Advertisements