Q:

Java program to find the (LCM) Lowest Common Multiple

belongs to collection: Java Basic Programs

0

Java program to find the (LCM) Lowest Common Multiple

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this program, we will read two integer numbers from the user and find the Lowest Common Multiple.

Program/Source Code:

The source code to find the LCM is given below. The given program is compiled and executed successfully.

// Java program to find the 
// Lowest Common Multiple

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner SC = new Scanner(System.in);

    int num1 = 0;
    int num2 = 0;
    int rem = 0;
    int lcm = 0;
    int X = 0;
    int Y = 0;

    System.out.printf("Enter Number1: ");
    num1 = SC.nextInt();

    System.out.printf("Enter Number2: ");
    num2 = SC.nextInt();

    if (num1 > num2) {
      X = num1;
      Y = num2;
    } else {
      X = num2;
      Y = num1;
    }

    rem = X % Y;

    while (rem != 0) {
      X = Y;
      Y = rem;
      rem = X % Y;
    }

    lcm = num1 * num2 / Y;
    System.out.printf("Lowest Common Multiple is: %d\n", lcm);
  }
}

Output:

Enter Number1: 10
Enter Number2: 225
Lowest Common Multiple is: 450

Explanation:

In the above program, we imported the "java.util.Scanner" package to read input from the user. And, created a public class Main. It contains a static method main().

The main() method is an entry point for the program. Here, we read two integer numbers from the user using the Scanner class. Then we calculated the Lowest Common Multiple (LCM) and printed the result.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to calculate the area of a triangle b... >>
<< Java program to find the (GCD) Greatest Common Div...