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 Construct K-D Tree for 2 Dimensional Data
Apache Commons Collections MapUtils
Using Java Assertions
Apache Tiles Integration with Spring MVC
A Guide to LinkedHashMap in Java
Request Method Not Supported (405) in Spring
Java Program to Implement Binomial Heap
Java Program to Perform Left Rotation on a Binary Search Tree
Serialization và Deserialization trong java
Java Program to Implement Stein GCD Algorithm
Introduction to Spring Cloud CLI
Spring Boot - Exception Handling
Hướng dẫn Java Design Pattern – Dependency Injection
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Java Program to Implement Ternary Search Algorithm
Java Program to Implement SynchronosQueue API
Overview of the java.util.concurrent
Convert Character Array to String in Java
Convert Hex to ASCII in Java
Giới thiệu JDBC Connection Pool
Java Program to Implement Self Balancing Binary Search Tree
Using Spring ResponseEntity to Manipulate the HTTP Response
Configuring a DataSource Programmatically in Spring Boot
Custom HTTP Header with the HttpClient
Giới thiệu thư viện Apache Commons Chain
Dynamic Proxies in Java
How to Implement Caching using Adonis.js 5
New Features in Java 13
Quick Guide to the Java StringTokenizer
Java Program to Implement Sieve Of Atkin
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Check If a File or Directory Exists in Java