Compute the greatest common divisor

The problem

Find the greatest common divisor of two integers n1 and n2 is as follows: First find d to be the minimum of n1 and n2, then check whether d, d-1, d-2, ..., 2, or 1 is a divisor for both n1 and n2 in this order. The first such common divisor is the greatest common divisor for n1 and n2. Write a program that prompts the user to enter two positive integers and displays the gcd.

Breaking it down

public static void main(String[] args) {

    Scanner input = new Scanner(;
    System.out.print("Enter first integer number: ");
    int num1 = input.nextInt();
    System.out.print("Enter second integer number: ");
    int num2 = input.nextInt();

    System.out.println("GCD of " + num1 + " and " + num2 + " is "
            - gcd(num1, num2));

static int gcd(int a, int b) {
    if (a == 0 || b == 0) {
        return a + b;
    return gcd(b, a % b);


Enter first integer number: 10
Enter second integer number: 20
GCD of 10 and 20 is 10