Home >> Java Programs >> Find LCM of Two Numbers Hey there, welcome back to another post. In this post, we will learn how to find the LCM of two numbers in Java. Java Program to Find LCM of Two Numbers The LCM (Least Common Multiple) is the smallest number that can be divided by both numbers. For example, the LCM of 5 and 10 is 10 because 10 is the smallest number that is divisible by both 5 and 10. Example 1: Input Number1: 8 Number2: 10 Output LCM of 8 and 10 is: 40 Find LCM of Two Numbers using GCD We can find the LCM of two numbers by using GCD. The relation between LCM and GCD is: a x b = LCM(a, b) * GCD (a, b) LCM(a, b) = (a x b) / GCD(a, b) For example, LCM(8, 10) is: LCM(8, 10) = ( 8 * 10) / GCD(8,10) LCM(8, 10) = 80 / 2 = 40 Java Program: import java.util.Scanner; /** * https://www.thecodingshala.com/ */ public class Main { public static int findGCD( int num1, int num2) { if (num1 == 0) {...
Comments
Post a Comment