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 Gradle Plugin
Java Program to Implement Binary Tree
Java Program to Compute Cross Product of Two Vectors
Spring Boot - Zuul Proxy Server and Routing
Java Program to Implement ArrayBlockingQueue API
Java Program to Implement Hash Tree
Dockerizing a Spring Boot Application
Java Program to Check Cycle in a Graph using Topological Sort
How to Read a File in Java
HttpClient 4 – Follow Redirects for POST
Java Program to Implement Sorted List
Quick Guide on Loading Initial Data with Spring Boot
Transaction Propagation and Isolation in Spring @Transactional
Java Program to Implement Selection Sort
Converting Between a List and a Set in Java
New Features in Java 11
Disable DNS caching
Spring @RequestParam Annotation
Java Deep Learning Essentials - Yusuke Sugomori
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
Hướng dẫn sử dụng Java Annotation
Hướng dẫn Java Design Pattern – Singleton
The HttpMediaTypeNotAcceptableException in Spring MVC
Hướng dẫn sử dụng Printing Service trong Java
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Java CyclicBarrier vs CountDownLatch
Java 8 – Powerful Comparison with Lambdas
A Custom Data Binder in Spring MVC
Enum trong java
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
The Order of Tests in JUnit