Skip to content

Commit 419814e

Browse files
committed
1281. Subtract the Product and Sum of Digits of an Integer
1 parent 490e77d commit 419814e

File tree

4 files changed

+237
-3
lines changed

4 files changed

+237
-3
lines changed

1281-Leetcode.md

+217
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
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+
```

1920-Leetcode.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Certainly! Here's the given code translated into C++, Python, Python3, C, C#, JavaScript, and TypeScript, along with the comments:
1+
© [Aryan Mishra](https://leetcode.com/hola_aryan/)
22

33
### C++
44

1929-Leetcode.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Certainly! Here's the given code translated into C++, Python, Python3, C, C#, JavaScript, and TypeScript, along with the comments:
1+
© [Aryan Mishra](https://leetcode.com/hola_aryan/)
22

33
### C++
44

guidelines.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,29 @@
22

33
## File naming
44

5-
<i>(Ques-no)(Dash-)(Leetcode)</i>
5+
<i>(Ques-no)(Dash-)(Leetcode)(.md)</i>
66

77
Please ensure your pull request adheres to the following guidelines:
88

99
- Make an individual pull request for each answer.
1010
- It would be helpful if you add the solution in all languages that you know.
1111
- Can suggest for different way to solve question as well.
12+
- Add your name and leetcode id at start of file
13+
14+
This is an example
15+
© [Your name](https://leetcode.com/Your_leetcode_id)
16+
17+
### C++ (Coding Language)
18+
19+
```cpp
20+
#include <vector>
21+
22+
class Solution {
23+
public:
24+
std::vector<int> getConcatenation(std::vector<int>& nums) {
25+
Your Code
26+
}
27+
};
28+
```
1229
1330
Thank you for your suggestions!

0 commit comments

Comments
 (0)