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:


import_contacts

You may also like:

Related Posts

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.