-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcm_gcd.java
38 lines (31 loc) · 949 Bytes
/
lcm_gcd.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
//https://www.geeksforgeeks.org/problems/lcm-and-gcd4516/1
import java.util.Scanner;
public class lcm_gcd {
public static int[] lcmAndGcd(int a, int b) {
int gcd = findGcd(a, b);
int lcm = (a * b) / gcd;
int[] result = new int[2];
result[0] = lcm;
result[1] = gcd;
return result;
}
public static int findGcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number for 'A': ");
int a = sc.nextInt();
System.out.println("Enter a number for 'b': ");
int b = sc.nextInt();
int[] result = lcmAndGcd(a, b);
System.out.println("LCM: " + result[0]);
System.out.println("GCD: " + result[1]);
sc.close();
}
}