-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab3.java
More file actions
41 lines (31 loc) · 1.14 KB
/
Lab3.java
File metadata and controls
41 lines (31 loc) · 1.14 KB
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
41
import static java.lang.Math.max;
import java.util.Scanner;
public class Lab3 {
//Q2
static int Multiply(int a, int b){
if(a<10 || b<10){
return (a*b);
}
int n=Math.max(Integer.toString(a).length(),Integer.toString(b).length());
int mid=n/2;
int high1=(int)(a/Math.pow(10,mid));
int low1=(int)(a%Math.pow(10,mid));
int high2=(int)(b/Math.pow(10,mid));
int low2=(int)(b%Math.pow(10,mid));
int highs=Multiply(high1,high2);
int mids=Multiply((high1+low1),(high2+low2));
int lows=Multiply(low1,low2);
int result=highs*(int)Math.pow(10,2*mid)+(mids-highs-lows)*(int)Math.pow(10,mid)+lows;
return(result);
}
public static void main(String[] args) {
int num1,num2;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
num1=sc.nextInt();
System.out.println("Enter the number");
num2=sc.nextInt();
int product=Multiply(num1, num2);
System.out.println("The product is"+" "+product);
}
}