In: Computer Science
import java.util.*;
class Main
{
static int gcd(int a, int b) //gcd method
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b) //Checking if a=b
return a;
if (a > b) //Checking if
a>b
return gcd(a-b,
b); //Recursively calling gcd method
return gcd(a, b-a); //Recursively
calling gcd method
}
public static void main(String[] args)
{
int a,b,ans=0; //Declaring
variables
Scanner sc = new
Scanner(System.in); //For taking input from user
System.out.println("Enter the first
number: ");
a = sc.nextInt(); //Taking input
from user
System.out.println("Enter the
second number: ");
b = sc.nextInt(); //Taking input
from user
ans = gcd(a,b); //Calling gcd
method and storing result in ans
System.out.println("GCD of " + a +"
and " + b + " is " + ans); //Printing the result
if(ans==1) //Checking if
ans=1
System.out.println(a+" and "+b+"
are relative prime"); //Printing if relative prime
}
}
Output:-