Java Program to Find GCD and LCM of Two Numbers Using Euclid’s Algorithm Last Updated : 04 Dec, 2020 Comments Improve Suggest changes Like Article Like Report GCD or the Greatest Common Divisor of two given numbers A and B is the highest number dividing both A and B completely, i.e., leaving remainder 0 in each case. LCM or the Least Common Multiple of two given numbers A and B is the Least number which can be divided by both A and B, leaving remainder 0 in each case. The LCM of two numbers can be computed in Euclid's approach by using GCD of A and B. LCM(A, B) = (a * b) / GCD(A, B) Examples: Input : A = 20, B = 30 Output: GCD = 10 LCM = 60 Explanation: The highest number which divides 20 and 30 is 10. So the GCD of 20, 30 is 10. The lowest number that can be divided by 20 and 30, leaving remainder 0 is 60. So the LCM of 20 and 30 is 60. Input : A= 33, B= 40 Output: GCD = 1 LCM = 1320 Euclidean Algorithm for Computing GCD:This approach of computing the GCD is based on the principle that the GCD of two numbers A and B remains the same even if the larger number is replaced by the modulo of A and B. In this approach, we perform the gcd operation on A and B repeatedly by replacing A with B and B with the modulo of A and B until the modulo becomes 0. Below is the implementation of to find GCD and LCM of Two Numbers Using Euclid’s Algorithm: Java // Java program to compute // GCD of two numbers // using Euclid's algorithm import java.io.*; class GFG { // 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)); } } OutputGCD = 10 LCM = 60 Comment More infoAdvertise with us Next Article Java Program to Find GCD and LCM of Two Numbers Using Euclid’s Algorithm ushashree Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Practice Tags : Java Similar Reads Java Program to Find LCM of Two Numbers LCM (i.e. Least Common Multiple) is the largest of the two stated numbers that can be divided by both the given numbers. In this article, we will write a program to find the LCM in Java Java Program to Find the LCM of Two NumbersThe easiest approach for finding the LCM is to Check the factors and th 2 min read Java Program to Find GCD or HCF of Two Numbers GCD (i.e. Greatest Common Divisor) or HCF (i.e. Highest Common Factor) is the largest number that can divide both the given numbers. Example: HCF of 10 and 20 is 10, and HCF of 9 and 21 is 3.Therefore, firstly find all the prime factors of both the stated numbers, then find the intersection of all t 2 min read Java Program for GCD of more than two (or array) numbers The GCD of three or more numbers equals the product of the prime factors common to all the numbers, but it can also be calculated by repeatedly taking the GCDs of pairs of numbers. gcd(a, b, c) = gcd(a, gcd(b, c)) = gcd(gcd(a, b), c) = gcd(gcd(a, c), b) Java // Java program to find GCD of two or // 1 min read Java Program to Compute GCD GCD (Greatest Common Divisor) of two given numbers A and B is the highest number that can divide both A and B completely, i.e., leaving the remainder 0 in each case. GCD is also called HCF(Highest Common Factor). There are various approaches to finding the GCD of two given numbers.Approaches: The GC 5 min read Java Program for Range LCM Queries Given an array of integers, evaluate queries of the form LCM(l, r). There might be many queries, hence evaluate the queries efficiently.  LCM (l, r) denotes the LCM of array elements that lie between the index l and r (inclusive of both indices) Mathematically, LCM(l, r) = LCM(arr[l], arr[l+1] , .. 4 min read Python Program to Find LCM of Two Numbers We are given two numbers and our task is to find the LCM of two numbers in Python. In this article, we'll discuss different approaches to finding the LCM of two numbers in Python.Example:Input: a = 12, b = 15Output: 60Explanation: LCM of 12 and 15 is 60Python Program to Find LCM of Two NumbersBelow 3 min read Java Program for Basic Euclidean algorithms GCD of two numbers is the largest number that divides both of them. A simple way to find GCD is to factorize both numbers and multiply common factors. Java // Java program to demonstrate working of extended // Euclidean Algorithm import java.util.*; import java.lang.*; class GFG { // extended Euclid 1 min read Java Program for Extended Euclidean algorithms GCD of two numbers is the largest number that divides both of them. A simple way to find GCD is to factorize both numbers and multiply common factors. Java // Java program to demonstrate working of extended // Euclidean Algorithm import java.util.*; import java.lang.*; class GFG { static public void 1 min read How to find the LCM of 4 numbers? Before going to find Least Common Multiple we have to know the number system. In this number system, we can learn natural numbers, whole numbers, Integers, and Rational numbers. If we have an idea about the number system we can be able to identify the type of number it is? so that we can find LCM ea 8 min read Java Guava | gcd(long a, long b) of LongMath Class with Examples The method gcd(long a, long b) of Guava's LongMath Class returns the greatest common divisor of two parameters a and b. Syntax: public static long gcd(long a, long b) Parameters: This method accepts two parameters a and b of the long type of whose GCD is to be found. Return Type: This method returns 2 min read Like