Java Program to find LCM of Two Numbers

Java Program to find LCM of Two Numbers | ninjasquad

In this example, You learn about Java program to find LCM of two numbers.

Here you learn find LCM of numbers in Java, You are given two numbers and the task of the program to find LCM/GCD in Java.

LCM(Lowest Common Factor)

Lowest Common Factor of two numbers is the smallest positive number that is divisible by both numbers. Example, LCM of 10 and 15 is 30 because 30 is the lowest number divisible by both 10 and 15.

GCD(Greatest Common Divisor/Highest Common Factor)

A Greatest Common Divisor of two numbers is the largest positive number and non zero numbers that is divisible by both numbers. Example, GCD of 24 and 36 is 12 because 12 is the greatest number divisible by both 24 and 36.

import java.util.*;

public class Main {
  public static void main(String[] args)
    Scanner scn = new Scanner(;
    System.out.println("Enter Value of A:");
    int n1 = scn.nextInt();
    System.out.println("Enter Value of B:");
    int n2 = scn.nextInt();
    int temp1 = n1;
    int temp2 = n2;

    while (n1 % n2 != 0)
      int remainder = n1 % n2;
      n1 = n2;
      n2 = remainder;
    int gcd = n2;
    int lcm = (temp1 * temp2) / gcd;

    System.out.println("GCD :"+gcd);
    System.out.println("LCM :"+lcm);


Enter Value of A:36
Enter Value of B:24
GCD :12
LCM :72

In the above article, I shared Java program to find LCM/GCD of Two Numbers. If you know something new then please comment or send mail to featured and for contribution to and I also share java program soon.

Happy Coding!! 😊

Source: Internet

Leave a Comment

We are offering free coding tuts