Java Program to find LCM of Two Numbers

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.in);
    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);
  }
}

Output


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 codeamy.in and I also share java program soon.


Happy Coding!! 😊

1 Comments

If you have any doubts, Please let me know

  1. Nylon can be combined with many components to create completely different variants and materials properties. Steel to make a plastics mould as opposed to aluminumfor a number of} causes, including durability and the ability to injection plastic mould advanced parts. While P-20 is taken into account the workhorse of the business, we may even use different metal types depending on the state of affairs and necessities. Reinforced response injection precision machining molding is a RIM course of during which reinforcing agents such as glass or carbon fibers are blended with the liquid polymers.

    ReplyDelete
Previous Post Next Post

Contact Form