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:
Guide to Spring 5 WebFlux
Java Program to Find Minimum Element in an Array using Linear Search
Introduction to Project Reactor Bus
Java Program to Implement the Hungarian Algorithm for Bipartite Matching
Java Program to Perform Partial Key Search in a K-D Tree
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Spring REST API + OAuth2 + Angular
Java Program to Perform Cryptography Using Transposition Technique
Predicate trong Java 8
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Hướng dẫn Java Design Pattern – State
Spring Security Authentication Provider
Java Program to Solve any Linear Equations
Java Program to implement Sparse Vector
Apache Commons Collections MapUtils
Guide to @JsonFormat in Jackson
Java Program to Generate a Graph for a Given Fixed Degree Sequence
Shuffling Collections In Java
Java Program to Implement Stack using Linked List
A Custom Media Type for a Spring REST API
The Guide to RestTemplate
Batch Processing with Spring Cloud Data Flow
Java Program to Implement LinkedHashMap API
Java Program to Implement Depth-limited Search
Ignore Null Fields with Jackson
Java Program to Implement the Program Used in grep/egrep/fgrep
Creating Docker Images with Spring Boot
A Guide to the ResourceBundle
Wrapper Classes in Java
Java Program to Implement Horner Algorithm
Form Validation with AngularJS and Spring MVC
Spring Boot - Internationalization