-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathHCF_and_LCM.java
More file actions
39 lines (36 loc) · 955 Bytes
/
HCF_and_LCM.java
File metadata and controls
39 lines (36 loc) · 955 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
import java.util.Scanner;
public class HCF_and_LCM {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter two numbers to find HCF and LCM: ");
int a = scan.nextInt();
int b = scan.nextInt();
int lcm = lcm(a,b);
int hcf = hcf(a,b);
System.out.println("HCF: " + hcf + " and LCM: " + lcm);
}
static int hcf(int a,int b){
int min = 0;
if(a>b)
min =b;
else
min =a;
for(int i=min; i>=2;i--){
if(a%i==0 && b%i==0){
return i;
}
}
return 0;
}
static int lcm(int a,int b){
int max = 0;
if(a<b)
max=b;
else max=a;
for(int i =max;i<=a*b;i++) {
if (i % a == 0 && i % b == 0)
return i;
}
return 0;
}
}