-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiply.java
More file actions
40 lines (27 loc) · 896 Bytes
/
Copy pathMultiply.java
File metadata and controls
40 lines (27 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package org.example;
public class Multiply {
public String multiply(String num1, String num2) {
int totalSum = 0;
for (int i = num2.length() - 1; i >= 0; i--) {
int curr = num2.charAt(i) - '0';
int currWeight = power(num2.length() - 1 - i);
int sum = 0;
for (int j = num1.length() - 1; j >= 0; j--) {
int currNum1 = num1.charAt(j) - '0';
int currWeightNum1 = power(num1.length() - 1 - j);
int temp = currNum1 * curr * currWeightNum1;
sum += temp;
}
int temp2 = sum * currWeight;
totalSum += temp2;
}
return String.valueOf(totalSum);
}
private int power(int n) {
int weight = 1;
for (int i = 0; i < n; i++) {
weight *= 10;
}
return weight;
}
}