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:

Introduction to the Java ArrayDeque
Hướng dẫn sử dụng String Format trong Java
Setting the Java Version in Maven
Uploading MultipartFile with Spring RestTemplate
Spring Boot - File Handling
Converting Between an Array and a Set in Java
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
Guide to the Fork/Join Framework in Java
Hướng dẫn Java Design Pattern – Proxy
Java Program to Find Basis and Dimension of a Matrix
Java Program to subtract two large numbers using Linked Lists
Guava Collections Cookbook
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
A Guide to LinkedHashMap in Java
Simple Single Sign-On with Spring Security OAuth2
Java Program to Implement PrinterStateReasons API
Quick Guide to Spring Controllers
Java Program to Perform LU Decomposition of any Matrix
Java Program to Implement Shoelace Algorithm
Java Program to Implement Randomized Binary Search Tree
Java Program to Solve the Fractional Knapsack Problem
Spring RequestMapping
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
Map to String Conversion in Java
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Spring Data JPA @Modifying Annotation
Deploy a Spring Boot WAR into a Tomcat Server
Spring Boot - Unit Test Cases
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Iterable to Stream in Java
Fixing 401s with CORS Preflights and Spring Security
Lớp Collections trong Java (Collections Utility Class)