|
| 1 | +© [Aryan Mishra](https://leetcode.com/hola_aryan/) |
| 2 | + |
| 3 | +1281. Subtract the Product and Sum of Digits of an Integer |
| 4 | + |
| 5 | +### C++ |
| 6 | + |
| 7 | +```cpp |
| 8 | +#include <iostream> |
| 9 | + |
| 10 | +class Solution { |
| 11 | +public: |
| 12 | + // Function to subtract the product and sum of digits of a number |
| 13 | + int subtractProductAndSum(int n) { |
| 14 | + // Initializing variables for multiplication, individual digits, and sum |
| 15 | + int mul = 1; |
| 16 | + int num = 0; |
| 17 | + int sum = 0; |
| 18 | + |
| 19 | + // Loop to extract digits and perform multiplication and addition |
| 20 | + while (n > 0) { |
| 21 | + num = n % 10; |
| 22 | + mul *= num; // Multiply the digit to the product |
| 23 | + sum += num; // Add the digit to the sum |
| 24 | + n = n / 10; // Remove the last digit |
| 25 | + } |
| 26 | + |
| 27 | + // Returning the result of subtraction |
| 28 | + return mul - sum; |
| 29 | + } |
| 30 | +}; |
| 31 | + |
| 32 | +int main() { |
| 33 | + Solution solution; |
| 34 | + std::cout << solution.subtractProductAndSum(123) << std::endl; |
| 35 | + return 0; |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +### Java |
| 40 | + |
| 41 | +```java |
| 42 | +class Solution { |
| 43 | + // Function to subtract the product and sum of digits of a number |
| 44 | + public int subtractProductAndSum(int n) { |
| 45 | + // Initializing variables for multiplication, individual digits, and sum |
| 46 | + int mul = 1; |
| 47 | + int num = 0; |
| 48 | + int sum = 0; |
| 49 | + |
| 50 | + // Loop to extract digits and perform multiplication and addition |
| 51 | + while (n > 0) { |
| 52 | + num = n % 10; |
| 53 | + mul *= num; // Multiply the digit to the product |
| 54 | + sum += num; // Add the digit to the sum |
| 55 | + n = n / 10; // Remove the last digit |
| 56 | + } |
| 57 | + |
| 58 | + // Returning the result of subtraction |
| 59 | + return mul - sum; |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +### Python |
| 65 | + |
| 66 | +```python |
| 67 | +class Solution: |
| 68 | + # Function to subtract the product and sum of digits of a number |
| 69 | + def subtractProductAndSum(self, n: int) -> int: |
| 70 | + # Initializing variables for multiplication, individual digits, and sum |
| 71 | + mul = 1 |
| 72 | + num = 0 |
| 73 | + sum = 0 |
| 74 | + |
| 75 | + # Loop to extract digits and perform multiplication and addition |
| 76 | + while n > 0: |
| 77 | + num = n % 10 |
| 78 | + mul *= num # Multiply the digit to the product |
| 79 | + sum += num # Add the digit to the sum |
| 80 | + n //= 10 # Remove the last digit |
| 81 | + |
| 82 | + # Returning the result of subtraction |
| 83 | + return mul - sum |
| 84 | +``` |
| 85 | + |
| 86 | +### Python3 |
| 87 | + |
| 88 | +```python |
| 89 | +class Solution: |
| 90 | + # Function to subtract the product and sum of digits of a number |
| 91 | + def subtractProductAndSum(self, n: int) -> int: |
| 92 | + # Initializing variables for multiplication, individual digits, and sum |
| 93 | + mul = 1 |
| 94 | + num = 0 |
| 95 | + sum = 0 |
| 96 | + |
| 97 | + # Loop to extract digits and perform multiplication and addition |
| 98 | + while n > 0: |
| 99 | + num = n % 10 |
| 100 | + mul *= num # Multiply the digit to the product |
| 101 | + sum += num # Add the digit to the sum |
| 102 | + n //= 10 # Remove the last digit |
| 103 | + |
| 104 | + # Returning the result of subtraction |
| 105 | + return mul - sum |
| 106 | +``` |
| 107 | + |
| 108 | +### C |
| 109 | + |
| 110 | +```c |
| 111 | +#include <stdio.h> |
| 112 | + |
| 113 | +// Function to subtract the product and sum of digits of a number |
| 114 | +int subtractProductAndSum(int n) { |
| 115 | + // Initializing variables for multiplication, individual digits, and sum |
| 116 | + int mul = 1; |
| 117 | + int num = 0; |
| 118 | + int sum = 0; |
| 119 | + |
| 120 | + // Loop to extract digits and perform multiplication and addition |
| 121 | + while (n > 0) { |
| 122 | + num = n % 10; |
| 123 | + mul *= num; // Multiply the digit to the product |
| 124 | + sum += num; // Add the digit to the sum |
| 125 | + n = n / 10; // Remove the last digit |
| 126 | + } |
| 127 | + |
| 128 | + // Returning the result of subtraction |
| 129 | + return mul - sum; |
| 130 | +} |
| 131 | + |
| 132 | +int main() { |
| 133 | + printf("%d\n", subtractProductAndSum(123)); |
| 134 | + return 0; |
| 135 | +} |
| 136 | +``` |
| 137 | +
|
| 138 | +### C# |
| 139 | +
|
| 140 | +```csharp |
| 141 | +public class Solution { |
| 142 | + // Function to subtract the product and sum of digits of a number |
| 143 | + public int SubtractProductAndSum(int n) { |
| 144 | + // Initializing variables for multiplication, individual digits, and sum |
| 145 | + int mul = 1; |
| 146 | + int num = 0; |
| 147 | + int sum = 0; |
| 148 | +
|
| 149 | + // Loop to extract digits and perform multiplication and addition |
| 150 | + while (n > 0) { |
| 151 | + num = n % 10; |
| 152 | + mul *= num; // Multiply the digit to the product |
| 153 | + sum += num; // Add the digit to the sum |
| 154 | + n = n / 10; // Remove the last digit |
| 155 | + } |
| 156 | +
|
| 157 | + // Returning the result of subtraction |
| 158 | + return mul - sum; |
| 159 | + } |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +### JavaScript |
| 164 | + |
| 165 | +```javascript |
| 166 | +class Solution { |
| 167 | + // Function to subtract the product and sum of digits of a number |
| 168 | + subtractProductAndSum(n) { |
| 169 | + // Initializing variables for multiplication, individual digits, and sum |
| 170 | + let mul = 1; |
| 171 | + let num = 0; |
| 172 | + let sum = 0; |
| 173 | + |
| 174 | + // Loop to extract digits and perform multiplication and addition |
| 175 | + while (n > 0) { |
| 176 | + num = n % 10; |
| 177 | + mul *= num; // Multiply the digit to the product |
| 178 | + sum += num; // Add the digit to the sum |
| 179 | + n = Math.floor(n / 10); // Remove the last digit |
| 180 | + } |
| 181 | + |
| 182 | + // Returning the result of subtraction |
| 183 | + return mul - sum; |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +const solution = new Solution(); |
| 188 | +console.log(solution.subtractProductAndSum(123)); |
| 189 | +``` |
| 190 | + |
| 191 | +### TypeScript |
| 192 | + |
| 193 | +```typescript |
| 194 | +class Solution { |
| 195 | + // Function to subtract the product and sum of digits of a number |
| 196 | + subtractProductAndSum(n: number): number { |
| 197 | + // Initializing variables for multiplication, individual digits, and sum |
| 198 | + let mul = 1; |
| 199 | + let num = 0; |
| 200 | + let sum = 0; |
| 201 | + |
| 202 | + // Loop to extract digits and perform multiplication and addition |
| 203 | + while (n > 0) { |
| 204 | + num = n % 10; |
| 205 | + mul *= num; // Multiply the digit to the product |
| 206 | + sum += num; // Add the digit to the sum |
| 207 | + n = Math.floor(n / 10); // Remove the last digit |
| 208 | + } |
| 209 | + |
| 210 | + // Returning the result of subtraction |
| 211 | + return mul - sum; |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +const solution = new Solution(); |
| 216 | +console.log(solution.subtractProductAndSum(123)); |
| 217 | +``` |
0 commit comments