Write a java program to find LCM of two numbers
Java program to find LCM of two numbers
In this program, LCM(Least common multiple) of two or more numbers is the least positive number that can be divided by both the numbers, without leaving the remainder. It is also known as Lowest common multiple, Least Common Denominator and smallest common multiple. It is denoted by LCM (a, b) or lcm (a, b) where a and b are two integers.
It is used when we add, subtract, or compare the fractions. While we perform addition or subtraction of the fractions, we find the LCM of the denominators and then solve the fraction. The LCM of the denominator is known as the Least Common Denominator (LCD).
- Program:
public class Main {
public static void main(String[] args) {
int a = 72, b = 120, lcm;
// maximum number between a and b is stored in lcm
lcm = (a > b) ? a : b;
while(true) {
if( lcm % a == 0 && lcm % b == 0 ) {
System.out.printf("The LCM of %d and %d is %d.", a, b, lcm);
break;
}
++lcm;
}
}
}
- Output:
The LCM of 72 and 120 is 360.
0 comments