In this example, you will learn how to Find the LCM value of two numbers entered by user in Java language.
Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | public class JavaExamples { public static void main(String[] args) { Scanner reader = new Scanner(System.in); int firstNumber, secondNumber; System.out.print("Enter first number: "); firstNumber= reader.nextInt(); System.out.print("Enter second number: "); secondNumber= reader.nextInt(); int lcm; // maximum number between n1 and n2 is stored in lcm lcm = (firstNumber > secondNumber) ? firstNumber : secondNumber; // Always true while(true) { if( lcm % firstNumber == 0 && lcm % secondNumber == 0 ) { System.out.printf("The LCM of %d and %d is %d.", firstNumber, secondNumber, lcm); break; } ++lcm; } } } |
Outout: