# Java Program to find LCM and HCF of any two integer numbers

Here we are explaining how to write a java program to find out the LCM and HCF of any two integer numbers. So first we have to read two integer numbers from the user and by using a `while loop` we will calculate the LCM and HCF.

## What is LCM?

LCM  means the Least Common Multiplier. The LCM of two integer numbers a and b are the lowest positive integer that can be divided by a and b.

Example: The LCM of 10 and 12 is shown below.

LCM(10,12) = 60

## What is HCF?

HCF  means the Highest common Factor. The HCF of two integer numbers a and b are the highest positive integer that can divide a and b.

Example: The HCF of 10 and 12 is shown below.

HCF(10,12) = 2

## How to implement the java program to find out HCF and LCM?

First, we have to declare the class LCM_HCF. Then declare the variables. Create an object of the scanner class and read numbers into the variable a and b. Assign the values of a & b to temporary variables temp_a & temp_b.

Find HCF of a,b by using a `while loop` with the condition temp_b !=0

• Save the value in temp_b to temp
• Do the mod operation with temp_a and temp_b and keep the result in temp_b
• Shift the value temp to temp_a

After the successful execution of the loop, the HCF of the numbers will be in variable temp_a.

Calculate LCM using the formula (a*b)/HCF. Then display HCF and LCM using `System.out.println()`

### ALGORITHM

STEP 1: Declare the class LCM_HCF with a public modifier.

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3: Declare the integer variables a, b, temp_a, temp_b, temp, HCF, LCM.

STEP 4: Read the first number into the variable a.

STEP 5: Read the second number into the variable b

STEP 6: Assign temp_a = atemp_b = b

STEP 7: By using a `while loop` check temp_b!=0 then assign

temp=temp_b

temp_b=temp_a % temp_b

temp_a=temp.

STEP 8: Assign HCF as temp_a.

STEP 9: Calculate LCM as (a*b)/HCF.

STEP 10: Display HCF and LCM

## Java Source Code

```                                          ```import java.util.Scanner;

public class LCM_HCF{
public static void main(String args[]){
int  a, b, temp_a, temp_b, temp, HCF, LCM;
Scanner sc = new Scanner(System.in);

System.out.println("Enter the first Number: ");
a= sc.nextInt();
System.out.println("Enter the Second Number: ");
b= sc.nextInt();
sc.close();

temp_a = a;
temp_b = b;

while(temp_b != 0){
temp = temp_b;
temp_b = temp_a % temp_b;
temp_a = temp;
}

HCF = temp_a;
LCM = (a*b)/HCF;

System.out.println("HCF = "+HCF);
System.out.println("LCM =  "+LCM);
}
}```
```

## OUTPUT

```Enter the first Number: 5
Enter the Second Number: 10
HCF = 5
LCM =10
```
VIEW ALL
VIEW ALL
VIEW ALL