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:

Java Program to Perform integer Partition for a Specific Case
Java Program to Implement Trie
A Guide to the ResourceBundle
Guide to Escaping Characters in Java RegExps
Java Program to Implement IdentityHashMap API
Guava CharMatcher
Converting Between a List and a Set in Java
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Java Program to Compute the Area of a Triangle Using Determinants
Spring Security Remember Me
Java Program to Implement Euler Circuit Problem
Spring Cloud – Tracing Services with Zipkin
Autoboxing và Unboxing trong Java
Remove HTML tags from a file to extract only the TEXT
A Guide to the ViewResolver in Spring MVC
Control the Session with Spring Security
Java Program to Implement Hash Tables with Double Hashing
Java Program to Perform Finite State Automaton based Search
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Initialize a HashMap in Java
Java Program to Implement Hamiltonian Cycle Algorithm
So sánh ArrayList và LinkedList trong Java
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Receive email using IMAP
Từ khóa static và final trong java
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
Java Program to Implement the String Search Algorithm for Short Text Sizes
Logging in Spring Boot
Java Program to Describe the Representation of Graph using Incidence Matrix
Overflow and Underflow in Java