- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Java Program to Find G.C.D and L.C.M of Two Numbers Using Euclid's Algorithm

**Euclid’s Algorithm** is an efficient algorithm which helps us to find G.C.D and L.C.M of two numbers. In this article, we are learning to write a Java program to find the G.C.D and L.C.M of two numbers using Euclid’s Algorithm.

## G.C.D. of two numbers

G. C. D, known as Greatest Common Divisor is the highest common factor that divides the two given numbers exactly. Now let us look into an example and calculate the G.C.D of a two numbers.

**Factors** − In mathematics, Factors are the numbers that can divide a given number.

**Ex** − 8 can be divided by 1,2,4,8. Therefore, 1, 2,4,8 are factors of 8.

### Example

Consider the numbers 8 and 16

Factors of 8 are: 1,2,4,8

Factors of 16 are: 1,2,4,8,16

The common factors that divides 8 and 16 is 1,2,4,8. Among the common factors the highest common factor is 8. Hence, 8 is the GCD of 8 and 16.

### L.C.M. of two numbers

L.C.M, known as the Least Common Multiple is the least number that can be divided by both the given numbers, i.e., the smallest common multiple.

**Multiples** − In mathematics, Multiples are the numbers that can be divided by a given number.

**Ex** − 8,16,24, 32, ... can be divided by 8. So 8,16,24,32 are multiples of 8.

### Example

Consider the numbers: 8 and 16

Multiples of 8 are 8,16,24, ...

Factors of 16 are 16,32,48, ...

The common multiple that can be divided by 8 and 16 are 16, 32, 48 and so on. The least common multiple of both numbers are 16. As a result, 16 is the L.C.M of 8 and 16.

We now are going to implement a program to find L.C.M and G.C.D of two numbers using Euclid’s Algorithm as it is an effective way to find L.C.M and G.C.D in less time.

Euclid’s Algorithm works on the principle that,

“ If a>b and a, b are two integers then gcd of a and b is same as gcd of b and remainder of a divided by b”.

### Euclid’s Algorithm

Let us consider a and b as two numbers

If

**b=0,**then we return**a**as the GCD of the two numbers.Else, we replace

**a**by**b**and b by the remainder of a, b and then recursively call the GCD function.After finding the GCD, we can find

**LCM(a, b) = (a*b) / GCD**

### Example

The first step is to divide the larger number (36) by the smaller number (24) and find the remainder. We can do this using the modulo operator (%).

36 % 24 = 12

The remainder is 12. We then divide the smaller number (24) by the remainder (12) and find the new remainder.

24 % 12 = 0

The remainder is 0, which means we have found the G.C.D of 24 and 36. The G.C.D is the last non-zero remainder, which is 12.

Therefore, the G.C.D of 24 and 36 is 12. We can confirm this by checking that 12 is a common divisor of 24 and 36, and that no larger number divides both 24 and 36 evenly.

The L.C.M. (24,36) = (24*36)/12 = 72.

The L.C.M. of 24 and 36 is 72.

### Algorithm

Initialize the two numbers.

Write a recursive method to find the G.C.D of the numbers.

Create a custom class object.

Call the recursive method from the main method using custom class object. Then find the L.C.M of the numbers using the G.C.D value.

### Example

In this example, we initialise two numbers and then create an object of GCD class and we call the GCD method by using the GCD class object and pass these two numbers to the method and get the gcd value and store it. Then, we calculate the lcm using multiplying two numbers and dividing with the gcd value. We then print the and lcm of two numbers.

//Java Program to find G.C.D and L.C.M of two numbers using Euclid’s Algorithm import java.util.*; class Gcd{ public int GCD(int number1, int number2) { if (number2 == 0) { return number1; } return GCD(number2, number1 % number2); } } public class Main { public static void main(String[] args) { int number_1 = 36; int number_2 = 24; Gcd gcdObject = new Gcd(); int gcd = gcdObject.GCD(number_1, number_2); System.out.println("G.C.D of "+" "+number_1+" & "+number_2 +" is: "+gcd); int lcm = (number_1 * number_2) / gcd; System.out.println("L.C.M of "+" "+number_1+" & "+number_2 +" is: "+lcm); } }

### Output

G.C.D of 36 & 24 is: 12 L.C.M of 36 & 24 is: 72

Thus, in this article we have calculated the G.C.D and L.C.M of tw m=numbers using Eculid’s Algorithm in Java Programming.