forked from zeel-codder/Recursion-Hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGCD_LCM.java
49 lines (41 loc) · 1.23 KB
/
GCD_LCM.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
41
42
43
44
45
46
47
48
49
import java.util.*;
/**
* Contributor🎅
* Name: dhruvil-shah
* Github:https://github.com/dhruvil-shah
* WebSite(optional):https://serene-knuth-aca1d4.netlify.app/
*/
/**
* 👉 Problem: GCD and LCM of two numbers
* 👑 Description: Find the GCD and LCM of two numbers
* 🎓 Explanation(optional):The Greatest Common Divisor (also
* known as GCD) is the largest number that divides evenly into each number in a
* given set of numbers.
* The Least Common Multiple (also known as LCM) is the smallest
* positive multiple that is common to two or more numbers.
*
* Example:
* GCD(12,18)=6
* LCM(12,18)=36
* Detailed Explanation can be found on https://calcworkshop.com/fractions/gcf-lcm/
*/
public class GCD_LCM {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt(),b=sc.nextInt();
int gcd=gcd(a, b),lcm=lcm(a,b);
System.out.println("GCD:"+gcd+" LCM:"+lcm);
}
//A simple school methodology for finding GCD
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
//Remember a*b=gcd(a,b)*lcm(a,b);
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
}