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 - Internationalization
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Giới thiệu luồng vào ra (I/O) trong Java
Java Program to Implement Ford–Fulkerson Algorithm
Java Program to Implement Threaded Binary Tree
Control the Session with Spring Security
Tạo số và chuỗi ngẫu nhiên trong Java
Converting String to Stream of chars
Working with Kotlin and JPA
Hướng dẫn sử dụng Java Generics
Spring Boot - Securing Web Applications
Quick Intro to Spring Cloud Configuration
Java Program to Construct an Expression Tree for an Prefix Expression
How to Manually Authenticate User with Spring Security
Java Byte Array to InputStream
Java Program to Implement Borwein Algorithm
Java Program to Generate All Possible Combinations of a Given List of Numbers
Java Program to Implement ArrayList API
Convert Hex to ASCII in Java
Introduction to Spring Boot CLI
String Operations with Java Streams
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Java Program to Implement CopyOnWriteArrayList API
Assertions in JUnit 4 and JUnit 5
Java Program to Implement Self Balancing Binary Search Tree
Spring Boot - Build Systems
Running Spring Boot Applications With Minikube
Java 9 Stream API Improvements
Java Program to Implement Max Heap
Java Program to Implement Sieve Of Sundaram
Tìm hiểu cơ chế Lazy Evaluation của Stream trong Java 8
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching