Java Program to Find the GCD and LCM of two Numbers

This is java program to find the gcd and lcm of given two numbers. GCD is calculated using Euclidean Algorithm. LCM is found using factorization method.

Here is the source code of the Java Program to Find the GCD and LCM of n Numbers. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

//This is sample program to calculate the GCD and LCM of two given numbers
import java.util.Scanner;
 
public class GCD_LCM 
{
    static int gcd(int x, int y)
    {
        int r=0, a, b;
        a = (x > y) ? x : y; // a is greater number
        b = (x < y) ? x : y; // b is smaller number
 
        r = b;
        while(a % b != 0)
        {
            r = a % b;
            a = b;
            b = r;
        }
        return r;
    }
 
    static int lcm(int x, int y)
    {
        int a;
        a = (x > y) ? x : y; // a is greater number
        while(true)
        {
            if(a % x == 0 && a % y == 0)
                return a;
            ++a;
        }	
    }
 
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the two numbers: ");
        int x = input.nextInt();
        int y = input.nextInt();
 
        System.out.println("The GCD of two numbers is: " + gcd(x, y));
        System.out.println("The LCM of two numbers is: " + lcm(x, y));
        input.close();		
    }
}

Output:

$ javac GCD_LCM.java
$ java GCD_LCM
 
Enter the two numbers: 
15
25
The GCD of two numbers is: 5
The LCM of two numbers is: 75
 
Enter the two numbers: 
5
8
The GCD of two numbers is: 1
The LCM of two numbers is: 40

Related posts:

Weak References in Java
Using the Map.Entry Java Class
Sending Emails with Java
Java Program to Represent Graph Using Adjacency Matrix
Concurrent Test Execution in Spring 5
Convert String to Byte Array and Reverse in Java
Lập trình đa luồng với Callable và Future trong Java
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Java Program to Implement the MD5 Algorithm
Java 8 Collectors toMap
Java Program to Implement Dijkstra’s Algorithm using Set
Java Program to Implement Floyd Cycle Algorithm
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Java Program to Check the Connectivity of Graph Using DFS
HttpClient Basic Authentication
Java Program to Perform Polygon Containment Test
Spring Boot Actuator
DistinctBy in the Java Stream API
Join and Split Arrays and Collections in Java
Hướng dẫn Java Design Pattern – Iterator
Java Streams vs Vavr Streams
How to Get a Name of a Method Being Executed?
Serverless Functions with Spring Cloud Function
Create a Custom Exception in Java
Guide to Java OutputStream
Java Program to Implement the Vigenere Cypher
Introduction to the Java NIO Selector
Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Java Program to find the maximum subarray sum O(n^2) time(naive method)
A Guide to Java SynchronousQueue
Java Program to do a Depth First Search/Traversal on a graph non-recursively