Here we are going to find GCD of two numbers in Java.

Find GCD of two Numbers :


import java.util.Scanner;

public class GCDExample {

    public static void main(String a[]) {

        int firstValue, secondValue, result = 0;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a value");
        firstValue = scanner.nextInt();
        System.out.println("Enter another value");
        secondValue = scanner.nextInt();
        while (firstValue != 0) {
            result = firstValue;
            firstValue = secondValue % firstValue;
            secondValue = result;
        }
        System.out.println("GCD for given two numbers : " + result);

    }
}

Output :

Enter a value : 9
Enter another value : 25
GCD for given two numbers : 1

Happy Learning 🙂