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 14 Record Keyword
Hướng dẫn Java Design Pattern – Template Method
Java Program to Perform the Shaker Sort
Java Program to Implement Disjoint Sets
Iterating over Enum Values in Java
Netflix Archaius with Various Database Configurations
Java Program to Perform Cryptography Using Transposition Technique
Spring Security Basic Authentication
Java Program to Construct an Expression Tree for an Postfix Expression
Java Program to Implement Borwein Algorithm
HTTP Authentification and CGI/Servlet
Spring REST API + OAuth2 + Angular
Java 8 StringJoiner
Getting Started with Forms in Spring MVC
Java Program to Implement WeakHashMap API
Các kiểu dữ liệu trong java
So sánh HashSet, LinkedHashSet và TreeSet trong Java
Java Program to Implement Pairing Heap
Convert XML to JSON Using Jackson
Exploring the Spring 5 WebFlux URL Matching
The Java 8 Stream API Tutorial
Java Program to Implement PriorityBlockingQueue API
Guide to the Synchronized Keyword in Java
Configure a RestTemplate with RestTemplateBuilder
Java Program to Implement Sorted Array
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Java Program to Implement Interpolation Search Algorithm
Java Program to Implement String Matching Using Vectors
Java Program to Find MST (Minimum Spanning Tree) using Kruskal’s Algorithm
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Comparing Two HashMaps in Java
Java Program to Implement Skew Heap