Java Program to Find GCD of Two Numbers

In this program, we will learn to code the Java Program to Find GCD of Two Numbers. Let’s understand more about GCD or Greatest Common Divisor and How to calculate the GCD of Two Numbers.

We will calculate the GCD of two numbers using the following approaches:

  • Using For Loop
  • Using While Loop

Now, let’s see the Java Program to Find GCD of Two Numbers Using For Loop.

Java Program to Find GCD of Two Numbers

import java.util.*;

public class Main{
    
    public static void main(String[] args){
        
     Scanner sc = new Scanner(System.in);
     System.out.println("Enter first number: ");
     int first = sc.nextInt();
     
     System.out.println("Enter second number: ");
     int second = sc.nextInt();
     
     int gcd=0;
     
    // Calculating GCD of two numbers
    for (int i = 1; i <= first && i <= second; i++){
        if (first % i == 0 && second % i == 0){
            gcd = i;
        }
    }
    
    System.out.println("GCD of "+first+" and "+second+" is "+gcd);
        
    }
    
}

Output

Enter first number: 10
Enter second number: 25
GCD of 10 and 25 is 5

How Does This Program Work ?

     Scanner sc = new Scanner(System.in);
     System.out.println("Enter first number: ");
     int first = sc.nextInt();
     
     System.out.println("Enter second number: ");
     int second = sc.nextInt();

In this program, we have taken the input of both numbers to find the GCD using the Scanner Class in Java.

    // Calculating GCD of two numbers
    for (int i = 1; i <= first && i <= second; i++){
        if (first % i == 0 && second % i == 0){
            gcd = i;
        }
    }

Then, we used for loop to calculate the gcd of two numbers. If the iteration of i is divisible by both the first number as well as the second number, then it is a factor of both numbers.

The value of i keeps increasing until it is equal to either ‘first‘ or ‘second‘. The highest factor computed is the gcd of two numbers. The GCD of two numbers gets stored in the gcd named variable.

    System.out.println("GCD of "+first+" and "+second+" is "+gcd);

Then, we have just displayed the GCD calculated using the System.out.println() function in Java.

This is the Java Program to Find the GCD of Two Numbers Using For Loop. Now, let’s see the Java program to find the GCD of Two Numbers using While loop.

Java Program to Find GCD of Two Numbers Using While Loop

import java.util.*;

public class Main{
    
    public static void main(String[] args){
        
     Scanner sc = new Scanner(System.in);
     System.out.println("Enter first number: ");
     int first = sc.nextInt();
     
     System.out.println("Enter second number: ");
     int second = sc.nextInt();
     
     int gcd=0;
     
    // Calculating gcd using while loop
    while (second != 0){
        int temp = second;
        second = first % second;
        first = temp;
    }
    
    gcd = first;
    
    System.out.println("The GCD is "+gcd);
        
    }
    
}

Output

Enter first number: 21
Enter second number: 28
The GCD is 7

Conclusion

I hope after going through this post, you understand the Java Program to Find GCD of Two Numbers.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Also Read:

Leave a Comment

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