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:
Java Program to Implement RenderingHints API
The “final” Keyword in Java
Java Program to Implement Word Wrap Problem
Java Program to Implement Hash Tables with Double Hashing
Spring Security with Maven
Java Program to Implement Singly Linked List
Giới thiệu Aspect Oriented Programming (AOP)
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Java Program to Generate N Number of Passwords of Length M Each
Using a Mutex Object in Java
Guide to the Java ArrayList
Hướng dẫn Java Design Pattern – Visitor
Java Scanner hasNext() vs. hasNextLine()
Spring Boot - Introduction
Java Program to Implement D-ary-Heap
File Upload with Spring MVC
Java Program to Check Cycle in a Graph using Topological Sort
Spring NoSuchBeanDefinitionException
Using a List of Values in a JdbcTemplate IN Clause
Java Program to Construct an Expression Tree for an Infix Expression
Hamcrest Collections Cookbook
A Guide to Spring Cloud Netflix – Hystrix
Comparing Objects in Java
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Java Program to Implement SimpeBindings API
Spring Boot - Web Socket
Test a REST API with Java
How to Replace Many if Statements in Java
Case-Insensitive String Matching in Java
Consuming RESTful Web Services
wait() and notify() Methods in Java
Chuyển đổi Array sang ArrayList và ngược lại