Skip to content

Commit e36e999

Browse files
author
Amit Kothiyal
committed
Added multiply using loop with string input.
1 parent 9ebe19b commit e36e999

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

src/com/jwetherell/algorithms/mathematics/Multiplication.java

+59
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,63 @@ public static String multiplyUsingFFT(String a, String b) {
124124
}
125125
return result.toString();
126126
}
127+
128+
public static String multiplyUsingLoopWithStringInput(String a,String b){
129+
ArrayList<Integer> first=new ArrayList();
130+
ArrayList<Integer> second=new ArrayList();
131+
}
132+
133+
public static String multiplyUsingLoopWithStringInput(String a,String b){
134+
int k,i,j,carry=0,rem,flag=0,lim1,lim2,mul;
135+
ArrayList<Integer> first=new ArrayList();
136+
ArrayList<Integer> second=new ArrayList();
137+
138+
for(char n:a.toCharArray()){
139+
first.add(n-'0');
140+
}
141+
for (char n:b.toCharArray()){
142+
second.add(n-'0');
143+
}
144+
145+
lim1=first.size()-1;
146+
lim2=second.size()-1;
147+
148+
ArrayList<Integer> res=new ArrayList<>(Collections.nCopies(first.size()+second.size(), 0));
149+
150+
for(i=0;i<=lim1;i++)
151+
{
152+
k=i;
153+
for(j=0;j<=lim2;j++)
154+
{
155+
mul=first.get(i)*second.get(j);
156+
res.set(k,res.get(k)+(mul/10));
157+
k++;
158+
res.set(k,res.get(k)+(mul%10));
159+
}
160+
}
161+
162+
for(i=(lim1+lim2)+1;i>=0;i--)
163+
{
164+
if(flag==1){
165+
res.set(i,res.get(i)+carry);
166+
flag=0;
167+
}
168+
169+
if(res.get(i)>=10 && i!=0)
170+
{
171+
rem=res.get(i)%10;
172+
carry=res.get(i)/10;
173+
res.set(i,rem);
174+
flag++;
175+
}
176+
}
177+
178+
StringBuilder sb = new StringBuilder();
179+
for (Integer s : res)
180+
{
181+
sb.append(s);
182+
}
183+
184+
return sb.toString();
185+
}
127186
}

0 commit comments

Comments
 (0)