This is a Java Program to Compute GCD. Greatest Common Divisor of a given set of numbers is the highest number which divides exactly every number of the given set.

Enter the two numbers as input. Now we use loops to find GCD of two given numbers.

Here is the source code of the Java Program to Compute GCD. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

import static java.lang.StrictMath.min;

import java.util.Scanner;

public class GCD

`{`

public static void main(String args[])

`{`

int a, b, hcf = 1;

Scanner s = new Scanner(System.in);

System.out.print("Enter First Number:");

a = s.nextInt();

System.out.print("Enter Second Number:");

b = s.nextInt();

int n = min(a,b);

for(int i = 2; i < n; i++)

`{`

while(a % i == 0 && b % i==0)

`{`

hcf = hcf * i;

a = a / i;

b = b / i;

`}`

`}`

System.out.println("Greatest Common Divisor:"+hcf);

`}`

`}`

Output:

$ javac GCD.java $ java GCD Enter First Number:24 Enter Second Number:16 Greatest Common Divisor:8

**Sanfoundry Global Education & Learning Series – 1000 Java Programs.**

Here’s the list of Best Reference Books in Java Programming, Data Structures and Algorithms.