-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGCDAndLCM.java
41 lines (38 loc) · 1.1 KB
/
GCDAndLCM.java
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 Data_Structure_And_Algorithm;
import java.util.Scanner;
public class GCDAndLCM {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int gcd = 0;
if(a <= b){
for (int i = 1; i <= a; i++) {
if(a%i == 0 && b%i == 0){
gcd = i;
}
}
}else {
for (int i = 1; i <= b; i++) {
if(b%i == 0 && a%i==0){
gcd = i;
}
}
}
int lcm = (a * b) / gcd;
System.out.println("gcd "+ gcd + " Lcm " + lcm);
//-----------------------------------------------------
// int n =scanner.nextInt();
// int n1 =scanner.nextInt();
// int on = n;
// int on1 =n1;
// while (n%n1!=0){
// int rem = n%n1;
// n = n1;
// n1 = rem;
// }
// System.out.println("Gcd "+n1);
// int lcm = (on*on1) / n1;
// System.out.println("LCM "+ lcm);
}
}