Java program to find HCF and LCM of two numbers

Java program to find HCF and LCM of two numbers. In the java program, find HCF and LCM; it will print the HCF and LCM numbers in the output of the user-entered number. The HCF of given numbers is the largest or greatest factor common to any two or more given natural numbers. When you multiply a number by a whole number, you get a multiple (greater than 0).

import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
int a, b, x, y, t, hcf, lcm;
Scanner scan = new Scanner(System.in);
System.out.print("Enter Two Numbers");
x = scan.nextInt();
y = scan.nextInt();
a = x;
b = y;
while(b != 0)
{
t = b;
b = a%b;
a = t;
}
hcf = a;
lcm = (x*y)/hcf;
System.out.print("HCF = " +hcf);
System.out.print("\nLCM = " +lcm);
}
}

 

Output:

Enter Two Numbers

45 56

HCF = 1

LCM = 2520

LCM and HCF Program in Java

The HCF (Highest Common Factor) of two or more numbers is the highest number among all the common factors of the given numbers, whereas the least common multiple (LCM) of two or more numbers is the smallest number among all the common multiples of the given numbers.

Difference between HCF and LCM in java programming language

Highest Common Factor
The greatest factor that is common to any two or more given natural numbers is known as the highest common factor (HCF). GCD is another name for GCD (Greatest Common Divisor). For example, the HCF of 8 is 8, while the HCF of 40 is 8.

Least Common Multiple

The LCM is an abbreviation for “least common multiple.” When you multiply a number by a whole number, you obtain a multiple (greater than 0). The smallest number that is a multiple of all the numbers in a group is called the Least Common Multiple (LCM). For example, the LCM of 16 and 20 is 80, which is the smallest number that is a multiple of both 16 and 20.

Applications of LCM and HCF in mathematics

What are the uses of LCM and HCF? To break things down into smaller chunks. Any number of sets of items should be evenly distributed into their largest grouping. To determine the number of persons we can invite.

Significance of HCF and LCM

The biggest factor present between two or more numbers is defined by the H.C.F., whereas the least number that is exactly divisible by two or more numbers is defined by the L.C.M. The greatest common factor (GCF) is also known as the greatest common factor (HCF), while the least common divisor (LCM) is also known as the least common divisor.