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:

Guide to the Synchronized Keyword in Java
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Java Program to Describe the Representation of Graph using Incidence Matrix
Spring 5 Functional Bean Registration
Java Program to Implement Efficient O(log n) Fibonacci generator
Guide to the Java ArrayList
Java Program to Delete a Particular Node in a Tree Without Using Recursion
Java Program to Use rand and srand Functions
LinkedHashSet trong java
Java – Get Random Item/Element From a List
Java Program to Implement PrinterStateReasons API
Inheritance and Composition (Is-a vs Has-a relationship) in Java
ClassNotFoundException vs NoClassDefFoundError
Transactions with Spring and JPA
Java Program to Check Cycle in a Graph using Graph traversal
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
Spring Security – security none, filters none, access permitAll
Spring Data JPA Delete and Relationships
Write/Read cookies using HTTP and Read a file from the internet
Java Program to Implement Word Wrap Problem
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Java Program to Perform integer Partition for a Specific Case
Jackson – Decide What Fields Get Serialized/Deserialized
Composition, Aggregation, and Association in Java
Spring Boot - Exception Handling
Display Auto-Configuration Report in Spring Boot
Guide to Dynamic Tests in Junit 5
Spring Boot Change Context Path
Spring RestTemplate Error Handling
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Object Type Casting in Java
Using the Map.Entry Java Class