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 Security Logout
Wrapper Classes in Java
Java Program to Represent Linear Equations in Matrix Form
Refactoring Design Pattern với tính năng mới trong Java 8
Java Program to Implement Gale Shapley Algorithm
Java Program to Implement Insertion Sort
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
An Example of Load Balancing with Zuul and Eureka
Java Program to Perform Finite State Automaton based Search
Java 8 Stream findFirst() vs. findAny()
Entity To DTO Conversion for a Spring REST API
LinkedHashSet trong Java hoạt động như thế nào?
Java Program to implement Array Deque
Java Program to Implement Binomial Heap
Java Program to Find MST (Minimum Spanning Tree) using Kruskal’s Algorithm
Toán tử trong java
Java Program to Implement Stack using Linked List
Lớp Properties trong java
Hướng dẫn Java Design Pattern – Mediator
Jackson – Decide What Fields Get Serialized/Deserialized
Java – Get Random Item/Element From a List
Sử dụng CyclicBarrier trong Java
Quick Guide to Spring MVC with Velocity
Prevent Brute Force Authentication Attempts with Spring Security
Spring Security Form Login
A Guide to Java HashMap
Java Program to Implement Leftist Heap
HttpClient Basic Authentication
Java Program to Implement Nth Root Algorithm
New Features in Java 15
Collect a Java Stream to an Immutable Collection
Java Program to Construct K-D Tree for 2 Dimensional Data