You are on page 1of 3

1)

package demo.java;/**
** 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");
/** Make an object of EuclidGcd
class **/
EuclidGcd eg = new EuclidGcd();

/** Accept two integers **/


System.out.println("Enter first
integer numbers");
long n1 = scan.nextLong();
System.out.println("Enter
second integer numbers");
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);
}
}

You might also like