Java Program to Compute GCD
Last Updated :
04 Apr, 2025
GCD (Greatest Common Divisor) of two given numbers A and B is the highest number that can divide both A and B completely, i.e., leaving the remainder 0 in each case. GCD is also called HCF(Highest Common Factor). There are various approaches to finding the GCD of two given numbers.
Approaches:
The GCD of the given two numbers A and B can be calculated using different approaches.
- General method
- Euclidean algorithm (by repeated subtraction)
- Euclidean algorithm (by repeated division)
Examples:
Input: 20, 30
Output: GCD(20, 30) = 10
Explanation: 10 is the highest integer which divides both 20 and 30, leaving the remainders as zero
Input: 36, 37
Output: GCD(36, 37) = 1
Explanation: 36 and 37 don’t have any factors in common except 1. So, 1 is the gcd of 36 and 37
Note: gcd(A, B) = 1 if A, B are co-primes.
General Approach:
In the general approach of computing GCD, we actually implement the definition of GCD.
- First, find out all the factors of A and B individually.
- Then list out those factors which are common for both A and B.
- The highest of those common factors is the GCD of A and B.
Example:
A = 20, B = 30
Factors of A : (1, 2, 4, 5, 10, 20)
Factors of B : (1, 2, 3, 5, 6, 10, 15, 30)
Common factors of A and B : (1, 2, 5, 10)
Highest of the Common factors (GCD) = 10
It is clear that the GCD of 20 and 30 can’t be greater than 20. So we have to check for the numbers within the range 1 and 20. Also, we need the greatest of the divisors. So, iterate from backward to reduce computation time.
Java
// Java program to compute GCD of
// two numbers using general
// approach
import java.io.*;
class Geeks {
// gcd() method, returns the GCD of a and b
static int gcd(int a, int b)
{
// stores minimum(a, b)
int i;
if (a < b)
i = a;
else
i = b;
// take a loop iterating through smaller number to 1
for (i = i; i > 1; i--) {
// check if the current value of i divides both
// numbers with remainder 0 if yes, then i is
// the GCD of a and b
if (a % i == 0 && b % i == 0)
return i;
}
// if there are no common factors for a and b other
// than 1, then GCD of a and b is 1
return 1;
}
// Driver method
public static void main(String[] args)
{
int a = 30, b = 20;
// calling gcd() method over
// the integers 30 and 20
System.out.println("GCD = " + gcd(b, a));
}
}
Euclidean algorithm (repeated subtraction):
This approach is based on the principle that the GCD of two numbers A and B will be the same even if we replace the larger number with the difference between A and B. In this approach, we perform GCD operation on A and B repeatedly by replacing A with B and B with the difference(A, B) as long as the difference is greater than 0.
Example
A = 30, B = 20
gcd(30, 20) -> gcd(A, B)
gcd(20, 30 – 20) = gcd(20,10) -> gcd(B,B-A)
gcd(30 – 20, 20 – (30 – 20)) = gcd(10, 10) -> gcd(B – A, B – (B – A))
gcd(10, 10 – 10) = gcd(10, 0)
here, the difference is 0
So stop the procedure. And 10 is the GCD of 30 and 20
Java
// Java program to compute GCD
// of two numbers using Euclid's
// repeated subtraction approach
import java.io.*;
class Geeks {
// gcd method returns the GCD of a and b
static int gcd(int a, int b)
{
// if b=0, a is the GCD
if (b == 0)
return a;
// call the gcd() method recursively by
// replacing a with b and b with
// difference(a,b) as long as b != 0
else
return gcd(b, Math.abs(a - b));
}
// Driver method
public static void main(String[] args)
{
int a = 30, b = 20;
// calling gcd() over
// integers 30 and 20
System.out.println("GCD = " + gcd(a, b));
}
}
Euclidean algorithm (repeated division):
This approach is similar to the repeated subtraction approach. But, in this approach, we replace B with the modulus of A and B instead of the difference.
Example :
A = 30, B = 20
gcd(30, 20) -> gcd(A, B)
gcd(20, 30 % 20) = gcd(20, 10) -> gcd(B, A % B)
gcd(10, 20 % 10) = gcd(10, 0) -> gcd(A % B, B % (A % B))
here, the modulus became 0
So, stop the procedure. And 10 is the GCD of 30 and 20
Java
// Java program to compute GCD
// of two numbers using Euclid's
// repeated division approach
import java.io.*;
import java.util.*;
class Geeks {
// gcd method returns the GCD of a and b
static int gcd(int a, int b)
{
// if b=0, a is the GCD
if (b == 0)
return a;
// call the gcd() method recursively by
// replacing a with b and b with
// modulus(a,b) as long as b != 0
else
return gcd(b, a % b);
}
// Driver method
public static void main(String[] args)
{
int a = 20, b = 30;
// calling gcd() over
// integers 30 and 20
System.out.println("GCD = " + gcd(a, b));
}
}
Euclid’s repeated division approach is most commonly used among all the approaches.
Time complexity: O(log(min(a,b)))
Auxiliary space: O(log(min(a,b)) for recursive call stack
Note: In the above examples, we can also use the in-built method to find the minimum of two numbers Math.min(). But internally it will slightly take more performance overhead which can be negligible for normal use cases.
Similar Reads
Java Program for Common Divisors of Two Numbers
Given two integer numbers, the task is to find the count of all common divisors of given numbers? Input : a = 12, b = 24 Output: 6 // all common divisors are 1, 2, 3, // 4, 6 and 12 Input : a = 3, b = 17 Output: 1 // all common divisors are 1 Input : a = 20, b = 36 Output: 3 // all common divisors a
2 min read
Java Program to Implement the RSA Algorithm
RSA or RivestâShamirâAdleman is an algorithm employed by modern computers to encrypt and decrypt messages. It is an asymmetric cryptographic algorithm. Asymmetric means that there are two different keys. This is also called public-key cryptography because one among the keys are often given to anyone
3 min read
Java Program to Find GCD or HCF of Two Numbers
GCD (i.e. Greatest Common Divisor) or HCF (i.e. Highest Common Factor) is the largest number that can divide both the given numbers. Example: HCF of 10 and 20 is 10, and HCF of 9 and 21 is 3.Therefore, firstly find all the prime factors of both the stated numbers, then find the intersection of all t
2 min read
Java Program to Implement Pollard Rho Algorithm
Pollard's rho algorithm is an algorithm for integer factorization. It is particularly effective at splitting composite numbers with small factors. The Rho algorithmâs most remarkable success was the factorization of eighth Fermat number: 1238926361552897 * 9346163971535797776916355819960689658405123
2 min read
Java Program for GCD of more than two (or array) numbers
The GCD of three or more numbers equals the product of the prime factors common to all the numbers, but it can also be calculated by repeatedly taking the GCDs of pairs of numbers. gcd(a, b, c) = gcd(a, gcd(b, c)) = gcd(gcd(a, b), c) = gcd(gcd(a, c), b) Java Code // Java program to find GCD of two o
1 min read
Java Program for Extended Euclidean algorithms
GCD of two numbers is the largest number that divides both of them. A simple way to find GCD is to factorize both numbers and multiply common factors. Java Code // Java program to demonstrate working of extended // Euclidean Algorithm import java.util.*; import java.lang.*; class GFG { static public
1 min read
Java Program to Find LCM of Two Numbers
LCM (i.e. Least Common Multiple) is the largest of the two stated numbers that can be divided by both the given numbers. In this article, we will write a program to find the LCM in Java Java Program to Find the LCM of Two NumbersThe easiest approach for finding the LCM is to Check the factors and th
2 min read
Java Program to Check if count of divisors is even or odd
Given a number "n", find its total number of divisors is even or odd. Examples: Input: n = 10 Output: EvenInput: n = 100Output: OddInput: n = 125Output: EvenA naive approach would be to find all the divisors and then see if the total number of divisors is even or odd. The time complexity for such a
4 min read
Java Program for Basic Euclidean algorithms
GCD of two numbers is the largest number that divides both of them. A simple way to find GCD is to factorize both numbers and multiply common factors. // Java program to demonstrate working of extended // Euclidean Algorithm import java.util.*; import java.lang.*; class GFG { // extended Euclidean A
1 min read
Java Program to Find sum of even factors of a number
Given a number n, the task is to find the even factor sum of a number. Examples: Input : 30 Output : 48 Even dividers sum 2 + 6 + 10 + 30 = 48 Input : 18 Output : 26 Even dividers sum 2 + 6 + 18 = 26 Let p1, p2, ⦠pk be prime factors of n. Let a1, a2, .. ak be highest powers of p1, p2, .. pk respect
3 min read