LCM Program in Java Using For Loop

LCM Program in Java Using For Loop. 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).

LCM Definition

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

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. These are basic definition of LCM of Two Numbers in Java for beginners.

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.

public class Main
{
public static void main(String[] args)
{
int n1 = 45, n2 = 56, gcd = 1;
for(int i = 1; i <= n1 && i <= n2; ++i)
{
if(n1 % i == 0 && n2 % i == 0)
gcd = i;
}
int lcm = (n1 * n2) / gcd;
System.out.printf("LCM Number is %d.", lcm);
}
}

Output:

Enter Two Numbers

45 56

HCF = 1

LCM = 2520