- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- Maximum sum and product of the M consecutive digits in a number in C++
- Python program to find the sum of all even and odd digits of an integer list
- C++ program to Zoom digits of an integer
- Difference between product and sum of digits of a number in JavaScript
- Product sum difference of digits of a number in JavaScript
- Count even and odd digits in an Integer in C++
- Maximum of sum and product of digits until number is reduced to a single digit in C++
- Find last k digits in product of an array numbers in C++
- Find the Largest number with given number of digits and sum of digits in C++
- A two-digit number is 4 times the sum of its digits and twice the product of the digits. Find the number.
- A two digit number is 4 times the sum of its digits and twice the product of its digits. Find the number.
- Find smallest number with given number of digits and sum of digits in C++
- Swift Program to Count Number of Digits in an Integer
- Java Program to Count Number of Digits in an Integer
- Kotlin Program to Count Number of Digits in an Integer

Advertisements