BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to Find GCD of Two Numbers

Last Updated: September 8, 2018 by Chaitanya Singh | Filed Under: Java Examples

The GCD (Greatest Common Divisor) of two numbers is the largest positive integer number that divides both the numbers without leaving any remainder. For example. GCD of 30 and 45 is 15. GCD also known as HCF (Highest Common Factor). In this tutorial we will write couple of different Java programs to find out the GCD of two numbers.

How to find out the GCD on paper?

To find out the GCD of two numbers we multiply the common factors as shown in the following diagram:
Finding GCD of numbers in Java

Example 1: Java Program to find GCD of two numbers using for loop

In this example, we are finding the GCD of two given numbers using for loop.

We are running a for loop from 1 to the smaller number and inside loop we are dividing both the numbers with the loop counter “i” which ranges from 1 to the smaller number value. If the value of i divides both numbers with no remainder then we are assigning that value to the variable “gcd”. At the end of the loop, the variable “gcd” will have the largest number that divides both the numbers without remainder.

public class GCDExample1 {

    public static void main(String[] args) {

    	//Lets take two numbers 55 and 121 and find their GCD
        int num1 = 55, num2 = 121, gcd = 1;

        /* loop is running from 1 to the smallest of both numbers
         * In this example the loop will run from 1 to 55 because 55
         * is the smaller number. All the numbers from 1 to 55 will be 
         * checked. A number that perfectly divides both numbers would
         * be stored in variable "gcd". By doing this, at the end, the 
         * variable gcd will have the largest number that divides both
         * numbers without remainder.
         */
        for(int i = 1; i <= num1 && i <= num2; i++)
        {
            if(num1%i==0 && num2%i==0)
                gcd = i;
        }

        System.out.printf("GCD of %d and %d is: %d", num1, num2, gcd);
    }

}

Output:

GCD of 55 and 121 is: 11

Example 2: Finding GCD of two numbers using while loop

Lets write the same program using while loop. Here we are taking a different approach of finding gcd. In this program we are subtracting the smaller number from the larger number until they both become same. At the end of the loop the value of the numbers would be same and that value would be the GCD of these numbers.

public class GCDExample2 {

    public static void main(String[] args) {

        int num1 = 55, num2 = 121;

        while (num1 != num2) {
        	if(num1 > num2)
                num1 = num1 - num2;
            else
                num2 = num2 - num1;
        }

        System.out.printf("GCD of given numbers is: %d", num2);
    }

}

Output:

GCD of given numbers is: 11

Example 3: Finding GCD of two input(Entered by user) numbers

In this example, we are using Scanner to get the input from user. The user would enter the value of both the numbers and program would find the GCD of these entered numbers.

import java.util.Scanner;
public class GCDExample3 {

    public static void main(String[] args) {

        int num1, num2;
        
        //Reading the input numbers
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter first number:");
        num1 = (int)scanner.nextInt();
        
        System.out.print("Enter second number:");
        num2 = (int)scanner.nextInt();
        
        //closing the scanner to avoid memory leaks
        scanner.close();
        while (num1 != num2) {
        	if(num1 > num2)
                num1 = num1 - num2;
            else
                num2 = num2 - num1;
        }

        //displaying the result
        System.out.printf("GCD of given numbers is: %d", num2);
    }

}

Output:

Enter first number:30
Enter second number:250
GCD of given numbers is: 10

Here are a few related java examples:
1. Java program to find factorial
2. Java program to display Fibonacci series
3. Java program to find the largest number among three numbers

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Java Program to Find Factorial using For and While loop
  3. Java Program to find largest of three numbers using Ternary Operator
  4. Java Program to Calculate Simple Interest
  5. Java Program to Perform Arithmetic operation using Method Overloading

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Jerry says

    November 8, 2019 at 3:17 PM

    Is there a way to find the GCD of to numbers by using the modulo division operator only

    Reply
  2. Ronin says

    February 17, 2020 at 5:03 PM

    thanks dude really helpful for starting coders, much appreciated. :)

    Reply

Leave a Reply Cancel reply

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

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap