Java Program to Find the (LCM) Lowest Common Multiple

You are currently viewing Java Program to Find the (LCM) Lowest Common Multiple

Java program to find the (LCM) Lowest Common Multiple

// 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

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply