Given two numbers the task is to find the Least Common Multiple or Lowest Common Multiple.
Example: 6 and 15 have LCM of 30.
Here instead of Prime Factorization method we first calculate the GCD of 6 and 15 and then use formula LCM = (x*y)÷GCD. Where ‘x’ and ‘y’ are two numbers.
Java
import java.util.*;
public class LCM
{
public static void main(String args[])
{
int n1=0,n2=0,min=0,gcd=0,lcm=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter two numbers:-");
System.out.print("A: ");
n1=sc.nextInt();
System.out.print("B: ");
n2=sc.nextInt();
min=Math.min(n1,n2);
for(int i=1;i<=min;i++)
{
if(n1%i==0&&n2%i==0)
{
gcd=i;
}
}
lcm=(n1*n2)/gcd;
System.out.println("LCM of "+n1+" and "+n2+" is: "+lcm);
}
}Java