Java Program to Find the GCD of Two Numbers

By |2018-03-16T02:21:22+05:30June 22nd, 2016|java programs|

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.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 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment