-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem180.java
More file actions
40 lines (37 loc) · 907 Bytes
/
Copy pathProblem180.java
File metadata and controls
40 lines (37 loc) · 907 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 hard.method;
import java.util.Scanner;
// 최대공약수와, 최소공배수 구하기 (유클리드의 호제법 기억하기)
public class Problem180 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the 1st number : ");
int n = sc.nextInt();
System.out.print("Enter the 2st number : ");
int m = sc.nextInt();
int GCD = gcd(n, m);
int LCM = lcm(n, m);
System.out.printf("GCD of %d and %d is %d%n", n, m, GCD);
System.out.printf("LCM of %d and %d is %d", n, m, LCM);
sc.close();
}
public static int gcd(int n, int m) {
int num = n>m? m+1 : n+1;
while(num >= 2) {
num--;
if(n % num == 0 & m % num == 0) {
break;
}
}
return num;
}
public static int lcm(int n, int m) {
int num = n>m? n : m;
while(true) {
if(num %n == 0 & num % m == 0) {
break;
}
num++;
}
return num;
}
}