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 Tutorial – Bootstrap a Simple Application
Giới thiệu HATEOAS
Java Program to Implement Expression Tree
Java Program to Find Inverse of a Matrix
Java Program to Implement Binary Heap
Java Program to Implement Binomial Heap
Java Program to Find Median of Elements where Elements are Stored in 2 Different Arrays
A Guide to BitSet in Java
Updating your Password
Converting a Stack Trace to a String in Java
How to Store Duplicate Keys in a Map in Java?
Generate Spring Boot REST Client with Swagger
Java Program to Implement Radix Sort
Java – Write a Reader to File
Spring RestTemplate Request/Response Logging
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm
Hướng dẫn sử dụng lớp Console trong java
Spring Boot - Code Structure
Spring Data – CrudRepository save() Method
Custom Thread Pools In Java 8 Parallel Streams
Comparing Long Values in Java
Multipart Upload with HttpClient 4
JUnit 5 @Test Annotation
Spring Security 5 – OAuth2 Login
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
Spring Boot - Database Handling
Java Program to Implement Fenwick Tree
Java Program to Perform integer Partition for a Specific Case
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Spring Security Authentication Provider
Java Convenience Factory Methods for Collections