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 - File Handling
Create Java Applet to Simulate Any Sorting Technique
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Câu lệnh điều khiển vòng lặp trong Java (break, continue)
HTTP Authentification and CGI/Servlet
Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path
Java Program to Implement Dijkstra’s Algorithm using Set
Intro to Spring Boot Starters
Java Program to Implement Solovay Strassen Primality Test Algorithm
Using Spring @ResponseStatus to Set HTTP Status Code
Spring Boot with Multiple SQL Import Files
How to Read HTTP Headers in Spring REST Controllers
Hướng dẫn Java Design Pattern – Facade
Java Program to Implement Sorted Singly Linked List
Java Program to Generate a Sequence of N Characters for a Given Specific Case
Guide to BufferedReader
Java Program to Implement Euler Circuit Problem
The Guide to RestTemplate
Spring Security Login Page with React
Java Program to Construct K-D Tree for 2 Dimensional Data
Refactoring Design Pattern với tính năng mới trong Java 8
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Checking for Empty or Blank Strings in Java
Lấy ngày giờ hiện tại trong Java
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
Primitive Type Streams in Java 8
Java Program to Implement Find all Back Edges in a Graph
So sánh ArrayList và Vector trong Java
Java Program to Implement Cubic convergence 1/pi Algorithm
A Guide to TreeSet in Java
Java Program to Implement Word Wrap Problem
Intro to Inversion of Control and Dependency Injection with Spring