How to find GCD and LCM of two numbers in java

In this post, we will see how to find Greatest common divisor(GCD) and Least Common Multiple(LCM) in java.
GCD is calculated using Euclidean algorithm and LCM is calculated using reduction by GCD

Eucid algo for calculating GCD is:

Lets say , there are two numbers , a and b so
GCD of two numbers = GCD (b,a%b) and GCD(a,0)=a

LCM can be calculated using reduction by GCD :

LCM of two numbers a and b = a * b/GCD(a,b)

Java program :

When you run above program, you will get following output:

Add Comment