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 implement Dynamic Array
Java Program to Implement Sieve Of Atkin
Summing Numbers with Java Streams
Java Program to Implement Stack using Linked List
Multi Dimensional ArrayList in Java
Guide to Spring 5 WebFlux
Java Program to Implement Shoelace Algorithm
HttpClient 4 – Follow Redirects for POST
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Converting a Stack Trace to a String in Java
Intersection of Two Lists in Java
Java Program to Perform integer Partition for a Specific Case
Introduction to the Functional Web Framework in Spring 5
HttpAsyncClient Tutorial
A Guide To UDP In Java
Java Program to Implement Jarvis Algorithm
XML Serialization and Deserialization with Jackson
Java Program to Implement Sorted Doubly Linked List
Java Program to Implement Attribute API
Spring Boot - Bootstrapping
Exception Handling in Java
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Java Program to Implement the MD5 Algorithm
Java Program to Implement Merge Sort Algorithm on Linked List
Java Program to Compute Determinant of a Matrix
Guide to Java 8 groupingBy Collector
Registration – Password Strength and Rules
Java Program to Check for balanced parenthesis by using Stacks
Java Program to Implement Sieve Of Sundaram
Spring WebClient Filters
Send an email using the SMTP protocol
Java String Conversions