Write a program in Java to accept two numbers and find the LCM of the two numbers.
( Chapter : Looping Control Structures )
I would like to inform you that comments of any user cannot be deleted uselessly or on request. Comments are deleted in extreme cases when they violate Brainly rules.
*Hope you liked the video*
Answers
We know that product of two numbers = HCF * LCM.
Then, LCM = (Product)/HCF.
Sample program to find LCM of two numbers:
import java.util.*;
class Brainly
{
public static void main(String args[])
{
int a,b,c,d,e,f,g;
Scanner demo = new Scanner(System.in);
System.out.println("Enter two numbers :");
a = demo.nextInt();
b = demo.nextInt();
if(a > b)
{
c = a;
d = b;
}
else
{
c = b;
d = a;
}
e = c % d;
while(e != 0)
{
c = d;
d = e;
e = c % d;
}
f = d;
g = a * b / f;
System.out.println("LCM = " +g);
}
}
Output:
Enter two numbers:
9 24
LCM = 72.
Hope it helps you!
public class LCM {
public static void main(String[] args) {
int n1 = 72, n2 = 120, lcm;
// maximum number between n1 and n2 is stored in lcm
lcm = (n1 > n2) ? n1 : n2;
// Always true
while(true)
{
if( lcm % n1 == 0 && lcm % n2 == 0 )
{
System.out.printf("The LCM of %d and %d is %d.", n1, n2, lcm);
break;
}
++lcm;
}
}
}