IN JAVA with output
The LCM of two numbers can be found as follows:
If the larger number is also a multiple of the smaller number, it is the LCM of the two numbers,
otherwise the next multiple of the larger number can be found and it can be checked, if it is a multiple of
the smaller number. This process can be continued till the LCM is found.
Write a program to find the LCM of two numbers
Answers
Answered by
2
Answer:
public class Main {
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;
}
}
}
Explanation:
Please foll!ow me I'll give you answers in all I.T related questions
Similar questions