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:

Lập trình hướng đối tượng (OOPs) trong java
Recommended Package Structure of a Spring Boot Project
Java Program to Implement Borwein Algorithm
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
How to Iterate Over a Stream With Indices
Encode/Decode to/from Base64
Java Program to Implement Stein GCD Algorithm
Spring Boot Tutorial – Bootstrap a Simple Application
Java Program to Implement HashTable API
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Hướng dẫn sử dụng lớp Console trong java
Java Program to Implement Wagner and Fisher Algorithm for online String Matching
Java 8 Collectors toMap
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Java Program to Implement SynchronosQueue API
Java Program to Find Nearest Neighbor for Static Data Set
Filtering and Transforming Collections in Guava
Java Program to Implement Dijkstra’s Algorithm using Set
Hamcrest Collections Cookbook
Consumer trong Java 8
Spring Security Login Page with React
A Comparison Between Spring and Spring Boot
Create a Custom Exception in Java
Java Program to Implement Sieve Of Atkin
Java Program to Implement Find all Cross Edges in a Graph
ThreadPoolTaskExecutor corePoolSize vs. maxPoolSize
Case-Insensitive String Matching in Java
Spring MVC + Thymeleaf 3.0: New Features
Using a List of Values in a JdbcTemplate IN Clause
Bootstrapping Hibernate 5 with Spring
Lớp LinkedHashMap trong Java