-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcmgcd.java
More file actions
41 lines (31 loc) · 819 Bytes
/
lcmgcd.java
File metadata and controls
41 lines (31 loc) · 819 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
41
// Java program to compute
// GCD of two numbers
// using Euclid's algorithm
import java.io.*;
class lcmgcd {
// gcd method returns the GCD of a and b
static int gcd(int a, int b) {
// if b=0, a is the GCD
if (b == 0)
return a;
// call the gcd() method recursively by
// replacing a with b and b with
// modulus(a,b) as long as b != 0
else
return gcd(b, a % b);
}
// lcm() method returns the LCM of a and b
static int lcm(int a, int b, int gcdValue)
{
return Math.abs(a * b) / gcdValue;
}
// Driver method
public static void main(String[] args) {
int a = 20, b = 30, gcdValue;
gcdValue = gcd(a, b);
// calling gcd() over
System.out.println("GCD = " + gcdValue);
// calling lcm() over integers 30 and 20
System.out.println("LCM = " + lcm(a, b, gcdValue));
}
}