public class LcmOfTwoNumbers
{
public static void main(String[] args)
{
int no1 = 12, no2 = 15;
int lcm = no1;
if (no2 > no1)
{
lcm = no2;
}
while (true)
{
if (lcm % no1 == 0 && lcm % no2 == 0)
{
System.out.println("LCM: " + lcm);
break;
}
lcm++;
}
}
}
LCM: 60
import java.util.Scanner;
public class LcmOfTwoNumbers
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter no1: ");
int no1 = scanner.nextInt();
System.out.print("Enter no2: ");
int no2 = scanner.nextInt();
int lcm = no1;
if (no2 > no1)
{
lcm = no2;
}
while (true)
{
if (lcm % no1 == 0 && lcm % no2 == 0)
{
System.out.println("LCM: " + lcm);
break;
}
lcm++;
}
}
}
Enter no1: 20 Enter no2: 25 LCM: 100
no1 = 12 and no2 = 15.
int lcm = no1;
if (no2 > no1) {
lcm = no2;
}
lcm to the larger of the two numbers.
lcm is divisible by both numbers.
lcm % no1 == 0 and lcm % no2 == 0, it is the LCM.
lcm by 1 and repeat until you find the LCM.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weβre here to make our tutorials better based on your thoughts and suggestions.