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:
Simple Single Sign-On with Spring Security OAuth2
Using Optional with Jackson
The Order of Tests in JUnit
Redirect to Different Pages after Login with Spring Security
Apache Commons Collections Bag
Introduction to Netflix Archaius with Spring Cloud
Java Program to find the maximum subarray sum using Binary Search approach
The DAO with Spring and Hibernate
Lớp lồng nhau trong java (Java inner class)
The “final” Keyword in Java
Java Concurrency Interview Questions and Answers
Number Formatting in Java
Java Program to Solve Tower of Hanoi Problem using Stacks
Java Program to Implement Pairing Heap
Sending Emails with Java
Java Program to Implement Counting Sort
Spring Boot - Tomcat Port Number
Lớp Properties trong java
A Comparison Between Spring and Spring Boot
Spring Boot With H2 Database
Java Program to Implement Max-Flow Min-Cut Theorem
Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
Từ khóa static và final trong java
Logging in Spring Boot
Java Program to Implement LinkedBlockingDeque API
Converting a Stack Trace to a String in Java
Java – String to Reader
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph
Introduction to Spliterator in Java
Guide To CompletableFuture
StringBuilder vs StringBuffer in Java
Spring Boot - Zuul Proxy Server and Routing