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:
Guide to java.util.concurrent.Locks
Spring Boot - Tomcat Deployment
Java Program to Check Multiplicability of Two Matrices
Removing all Nulls from a List in Java
Java – Create a File
Java Program to Implement Gabow Algorithm
Java IO vs NIO
Spring Boot - Service Components
Spring NoSuchBeanDefinitionException
Java Program to Implement Sieve Of Sundaram
Mảng (Array) trong Java
Java Program to Implement Aho-Corasick Algorithm for String Matching
Ép kiểu trong Java (Type casting)
A Guide to Java 9 Modularity
Spring JDBC
Introduction to Java 8 Streams
Spring Boot with Multiple SQL Import Files
Netflix Archaius with Various Database Configurations
Transaction Propagation and Isolation in Spring @Transactional
Java Program to Perform Right Rotation on a Binary Search Tree
An Intro to Spring Cloud Zookeeper
Java – File to Reader
Apache Commons Collections MapUtils
New Stream Collectors in Java 9
Java Program to Implement ConcurrentLinkedQueue API
Prevent Brute Force Authentication Attempts with Spring Security
Enum trong java
Java Program to Implement Hash Trie
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Java Program to Evaluate an Expression using Stacks
Java Program to subtract two large numbers using Linked Lists
Java Program to Implement AVL Tree