Java Program to Implement Euclid GCD Algorithm

This is a Java Program to implement Euclid’s GCD Algorithm. This is a program to find GCD (Greatest Common Divisor) of two numbers using Euclid’s Algorithm.

Algorithm is as follows :

function gcd(a, b)
    if b = 0
       return a
    else
       return gcd(b, a mod b)

Here is the source code of the Java program to implement Euclids GCD Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/**
 ** Java Program to Implement Euclid GCD Algorithm
 **/
 
import java.util.Scanner;
 
/** Class EuclidGcd **/
public class EuclidGcd    
{
    /** Function to calculate gcd **/
    public long gcd(long p, long q)
    {
        if (p % q == 0)
            return q;
        return gcd(q, p % q);
    }
    /** Main function **/
    public static void main (String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Euclid GCD Algorithm Test\n");
        /** Make an object of EuclidGcd class **/
        EuclidGcd eg = new EuclidGcd();
 
        /** Accept two integers **/
        System.out.println("Enter two integer numbers\n");
        long n1 = scan.nextLong();
        long n2 = scan.nextLong();
        /** Call function gcd of class EuclidGcd **/
        long gcd = eg.gcd(n1, n2);
        System.out.println("\nGCD of "+ n1 +" and "+ n2 +" = "+ gcd);
    }
}

Output:

Euclid GCD Algorithm Test
 
Enter two integer numbers
 
257184 800128
 
GCD of 257184 and 800128 = 28576

Related posts:

The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Checking for Empty or Blank Strings in Java
Java Program to Implement Triply Linked List
Guava – Join and Split Collections
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Java Program to Implement JobStateReasons API
Java Program to Check Whether Topological Sorting can be Performed in a Graph
Check If a String Is Numeric in Java
Spring Boot - Database Handling
Biến trong java
Java Program to Implement Extended Euclid Algorithm
Optional trong Java 8
Debugging Reactive Streams in Java
Java Program to Implement Ternary Search Tree
Java Program to Search for an Element in a Binary Search Tree
Java Program to Implement Splay Tree
Java Program to Implement Fibonacci Heap
Java Program to Solve a Matching Problem for a Given Specific Case
Java Program to Test Using DFS Whether a Directed Graph is Strongly Connected or Not
Performance Difference Between save() and saveAll() in Spring Data
REST Web service: Upload và Download file với Jersey 2.x
How to Find an Element in a List with Java
Guide to Escaping Characters in Java RegExps
Java Program to Implement Hash Tree
Java Program to Implement Trie
Java Program to Perform Left Rotation on a Binary Search Tree
Hướng dẫn sử dụng Java Generics
Giới thiệu SOAP UI và thực hiện test Web Service
Use Liquibase to Safely Evolve Your Database Schema
Giới thiệu Google Guice – Dependency injection (DI) framework
Model, ModelMap, and ModelAndView in Spring MVC
HttpClient 4 – Follow Redirects for POST