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.
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 those factors that are present in both the given numbers. And at last, return the product of elements that are in the intersection.
Note: GCD of the two stated numbers doesn’t alter if the smaller number is subtracted from the bigger number.
GCD of 100 and 88 is 4
Similarly, you can find the GCD or HCF of any two given numbers.
An efficient solution is to utilize a modulo operator in the Euclidean algorithm which is the foremost algorithm applied for this topic.
The GCD of 47 and 91 is: 1