How to find GCD and LCM of two numbers in java

Table of Contents

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:

Was this post helpful?


You may also like:

Related Posts

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.