© Aryan Mishra
- Subtract the Product and Sum of Digits of an Integer
#include <iostream>
class Solution {
public:
// Function to subtract the product and sum of digits of a number
int subtractProductAndSum(int n) {
// Initializing variables for multiplication, individual digits, and sum
int mul = 1;
int num = 0;
int sum = 0;
// Loop to extract digits and perform multiplication and addition
while (n > 0) {
num = n % 10;
mul *= num; // Multiply the digit to the product
sum += num; // Add the digit to the sum
n = n / 10; // Remove the last digit
}
// Returning the result of subtraction
return mul - sum;
}
};
int main() {
Solution solution;
std::cout << solution.subtractProductAndSum(123) << std::endl;
return 0;
}
class Solution {
// Function to subtract the product and sum of digits of a number
public int subtractProductAndSum(int n) {
// Initializing variables for multiplication, individual digits, and sum
int mul = 1;
int num = 0;
int sum = 0;
// Loop to extract digits and perform multiplication and addition
while (n > 0) {
num = n % 10;
mul *= num; // Multiply the digit to the product
sum += num; // Add the digit to the sum
n = n / 10; // Remove the last digit
}
// Returning the result of subtraction
return mul - sum;
}
}
class Solution:
# Function to subtract the product and sum of digits of a number
def subtractProductAndSum(self, n: int) -> int:
# Initializing variables for multiplication, individual digits, and sum
mul = 1
num = 0
sum = 0
# Loop to extract digits and perform multiplication and addition
while n > 0:
num = n % 10
mul *= num # Multiply the digit to the product
sum += num # Add the digit to the sum
n //= 10 # Remove the last digit
# Returning the result of subtraction
return mul - sum
class Solution:
# Function to subtract the product and sum of digits of a number
def subtractProductAndSum(self, n: int) -> int:
# Initializing variables for multiplication, individual digits, and sum
mul = 1
num = 0
sum = 0
# Loop to extract digits and perform multiplication and addition
while n > 0:
num = n % 10
mul *= num # Multiply the digit to the product
sum += num # Add the digit to the sum
n //= 10 # Remove the last digit
# Returning the result of subtraction
return mul - sum
#include <stdio.h>
// Function to subtract the product and sum of digits of a number
int subtractProductAndSum(int n) {
// Initializing variables for multiplication, individual digits, and sum
int mul = 1;
int num = 0;
int sum = 0;
// Loop to extract digits and perform multiplication and addition
while (n > 0) {
num = n % 10;
mul *= num; // Multiply the digit to the product
sum += num; // Add the digit to the sum
n = n / 10; // Remove the last digit
}
// Returning the result of subtraction
return mul - sum;
}
int main() {
printf("%d\n", subtractProductAndSum(123));
return 0;
}
public class Solution {
// Function to subtract the product and sum of digits of a number
public int SubtractProductAndSum(int n) {
// Initializing variables for multiplication, individual digits, and sum
int mul = 1;
int num = 0;
int sum = 0;
// Loop to extract digits and perform multiplication and addition
while (n > 0) {
num = n % 10;
mul *= num; // Multiply the digit to the product
sum += num; // Add the digit to the sum
n = n / 10; // Remove the last digit
}
// Returning the result of subtraction
return mul - sum;
}
}
class Solution {
// Function to subtract the product and sum of digits of a number
subtractProductAndSum(n) {
// Initializing variables for multiplication, individual digits, and sum
let mul = 1;
let num = 0;
let sum = 0;
// Loop to extract digits and perform multiplication and addition
while (n > 0) {
num = n % 10;
mul *= num; // Multiply the digit to the product
sum += num; // Add the digit to the sum
n = Math.floor(n / 10); // Remove the last digit
}
// Returning the result of subtraction
return mul - sum;
}
}
const solution = new Solution();
console.log(solution.subtractProductAndSum(123));
class Solution {
// Function to subtract the product and sum of digits of a number
subtractProductAndSum(n: number): number {
// Initializing variables for multiplication, individual digits, and sum
let mul = 1;
let num = 0;
let sum = 0;
// Loop to extract digits and perform multiplication and addition
while (n > 0) {
num = n % 10;
mul *= num; // Multiply the digit to the product
sum += num; // Add the digit to the sum
n = Math.floor(n / 10); // Remove the last digit
}
// Returning the result of subtraction
return mul - sum;
}
}
const solution = new Solution();
console.log(solution.subtractProductAndSum(123));