The Greatest Common Divisor (GCD), also known as the Greatest Common Factor (GCF), or Highest Common Factor (HCF), of two or more non-zero integers, is the largest positive integer that divides the numbers without a remainder.
Here is the source code of the Java Program to Find Greatest Common Divisor of two Numbers. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import static java.lang.Math.min;
public class Greatest_Common_Divisor
public static void main(String args)
Scanner s=new Scanner(System.in);
System.out.print("Enter First Number:");
System.out.print("Enter Second Number:");
while(a%i==0 && b%i==0)
System.out.println("Greatest Common Divisor:"+hcf);
$ javac Greatest_Common_Divisor.java $ java Greatest_Common_Divisor Enter First Number:18 Enter Second Number:22 Greatest Common Divisor:2
Sanfoundry Global Education & Learning Series 1000 Java Programs.