Skip to content

Commit 0cf2268

Browse files
Add lcm also along with gcd
Lcm can be very easily obtained by simple equation. n1*n2 = gcd*lcm
1 parent 04003e3 commit 0cf2268

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

gcd.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
public class gcd
1+
public class gcdAndLcm
22
{
33
// Recursive function to return gcd of a and b
44
static int gcd(int a, int b)
@@ -18,16 +18,23 @@ static int gcd(int a, int b)
1818
return gcd(a-b, b);
1919
return gcd(a, b-a);
2020
}
21+
22+
static int lcm(int a, int b)
23+
{
24+
return ((a*b)/gcd(a, b));
25+
}
2126

2227
// Driver method
2328
public static void main(String[] args)
2429
{
2530

2631
System.out.println(gcd(40,5));
27-
System.out.println(gcd(48,56));
32+
System.out.println(gcd(48,56));
33+
System.out.println(lcm(40,5));
34+
System.out.println(lcm(48,56));
2835

2936

3037

31-
}
38+
}
3239

3340
}

0 commit comments

Comments
 (0)