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:
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Build a REST API with Spring and Java Config
Disable Spring Data Auto Configuration
Java Program to Implement a Binary Search Tree using Linked Lists
Java Concurrency Interview Questions and Answers
Java Program to Check Whether Topological Sorting can be Performed in a Graph
Jackson Ignore Properties on Marshalling
Converting Iterator to List
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm
Debug a JavaMail Program
Spring Boot - Actuator
Service Registration with Eureka
Java Program to Implement Splay Tree
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
Spring Boot - Enabling Swagger2
Introduction to Spring Data JPA
Using the Map.Entry Java Class
Spring Boot Gradle Plugin
Using a Mutex Object in Java
Java Program to Implement Tarjan Algorithm
HttpClient Timeout
Java Program to Implement Double Order Traversal of a Binary Tree
Java 8 StringJoiner
Introduction to Spliterator in Java
Intro to the Jackson ObjectMapper
Cachable Static Assets with Spring MVC
Arrays.asList vs new ArrayList(Arrays.asList())
Hướng dẫn sử dụng Java Reflection
Java NIO2 Path API
Deploy a Spring Boot WAR into a Tomcat Server
Template Engines for Spring