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:
Spring Boot Security Auto-Configuration
Java CyclicBarrier vs CountDownLatch
Java Program to Implement AVL Tree
Java Program to Perform the Sorting Using Counting Sort
Hướng dẫn Java Design Pattern – Transfer Object
Hướng dẫn Java Design Pattern – Null Object
How to Read a Large File Efficiently with Java
Introduction to Spring Cloud Stream
Java Program to Implement Tarjan Algorithm
Spring Boot - Rest Controller Unit Test
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Spring Boot - Flyway Database
Java equals() and hashCode() Contracts
Custom HTTP Header with the HttpClient
Send email with JavaMail
Send an email using the SMTP protocol
Java Program to Find Transpose of a Graph Matrix
Java Program to Implement Cartesian Tree
Java Program to Implement AA Tree
Simple Single Sign-On with Spring Security OAuth2
Guide to Character Encoding
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Java Program to implement Bit Set
The StackOverflowError in Java
Integer Constant Pool trong Java
Java Program to Implement Miller Rabin Primality Test Algorithm
The Difference Between Collection.stream().forEach() and Collection.forEach()
Test a REST API with Java
Hướng dẫn Java Design Pattern – Prototype