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 @RequestParam Annotation
Java Byte Array to InputStream
Mix plain text and HTML content in a mail
Java – InputStream to Reader
How to Kill a Java Thread
Working with Kotlin and JPA
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
Java Program to add two large numbers using Linked List
A Comparison Between Spring and Spring Boot
Java Program to Generate All Possible Combinations Out of a, b, c, d, e
Using a Spring Cloud App Starter
Java 8 – Powerful Comparison with Lambdas
Java Program to Implement SimpeBindings API
Spring Boot - Apache Kafka
Chuyển đổi từ HashMap sang ArrayList
Java – Write a Reader to File
Java Program to Implement Ternary Search Tree
Java Program to Create the Prufer Code for a Tree
Cơ chế Upcasting và Downcasting trong java
Giới thiệu Java 8
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Spring Boot Actuator
How to Get All Dates Between Two Dates?
Jackson – JsonMappingException (No serializer found for class)
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Java Program to Find the Mode in a Data Set
Java Program to Solve any Linear Equations
Java Program to Implement Find all Cross Edges in a Graph
Java Program to Generate Random Numbers Using Middle Square Method
Java Program to Implement Shunting Yard Algorithm
A Guide to JUnit 5
Guide to Spring Cloud Kubernetes