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:

Apache Commons Collections BidiMap
Inject Parameters into JUnit Jupiter Unit Tests
Luồng Daemon (Daemon Thread) trong Java
wait() and notify() Methods in Java
Java Program to Implement Triply Linked List
Java Program to Implement Dijkstra’s Algorithm using Set
Java Program to Implement Pairing Heap
Java Program to Perform Addition Operation Using Bitwise Operators
HandlerAdapters in Spring MVC
Java Program to Represent Linear Equations in Matrix Form
Guide to java.util.concurrent.BlockingQueue
Java Program to Generate a Random UnDirected Graph for a Given Number of Edges
Java Program to Implement Borwein Algorithm
New Features in Java 11
Java Program to Perform Searching in a 2-Dimension K-D Tree
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
The Spring @Controller and @RestController Annotations
Java Program to Implement LinkedHashSet API
Guide to @JsonFormat in Jackson
An Intro to Spring Cloud Security
Guava – Join and Split Collections
Encode a String to UTF-8 in Java
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Spring Boot - Zuul Proxy Server and Routing
Java Program to Implement IdentityHashMap API
Remove All Occurrences of a Specific Value from a List
Java Program to implement Bi Directional Map
Java Program to Implement Self Balancing Binary Search Tree
Prevent Cross-Site Scripting (XSS) in a Spring Application
Java Program to Implement Trie
HttpClient Timeout
How to Read HTTP Headers in Spring REST Controllers