Friday , April 28 2017
Home / java programs / Java Program to Find the GCD of Two Numbers

Java Program to Find the GCD of Two Numbers

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

About chandrashekhar

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+ ,

Check Also

Java Program to Print Diamond Pattern

Here we are going to write a java program to print Diamond Pattern using numbers.

Leave a Reply

Your email address will not be published. Required fields are marked *