Here’s a Java program to find the GCD (Greatest Common Divisor) of two numbers:
import java.util.Scanner;
public class GCD {
public static void main(String[] args) {
int num1, num2, gcd;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
num2 = scanner.nextInt();
gcd = findGCD(num1, num2);
System.out.println("GCD of " + num1 + " and " + num2 + " is " + gcd);
}
public static int findGCD(int num1, int num2) {
if (num2 == 0) {
return num1;
}
return findGCD(num2, num1 % num2);
}
}
In this program, we first take user input for two numbers. We then call the `findGCD()` method to find their GCD.
The `findGCD()` method takes two integer parameters `num1` and `num2` and returns their GCD as an integer. We use the Euclidean algorithm to find the GCD. If `num2` is 0, then we return `num1` as the GCD. Otherwise, we recursively call the `findGCD()` method with `num2` and the remainder of `num1` divided by `num2` as arguments. We repeat this process until `num2` becomes 0.
At the end of the program, we print out the GCD of the two numbers.