Given two numbers the task is to find the Greatest Common Factor or Highest Common Factor using recursion.
Example: 36 and 60 have a GCD of 12 since it is the only maximum number by which 36 and 60 both are divisible.
Java
import java.util.*;
public class GCDRec
{
public int getGCD(int n1,int n2)
{
if(n2==0)
{
return n1;
}
else
{
return getGCD(n2,n1%n2);
}
}
public static void main(String args[])
{
int n1=0,n2=0;
Scanner sc=new Scanner(System.in);
GCDRec ob=new GCDRec();
System.out.println("Enter two numbers:-");
System.out.print("A: ");
n1=sc.nextInt();
System.out.print("B: ");
n2=sc.nextInt();
System.out.println("GCD of "+n1+" and "+n2+" is "+ob.getGCD(n1,n2));
}
}Java