Program for LCM and HCF |Java Program|

Introduction

    In mathematics, finding the Highest Common Factor (HCF) and the Least Common Multiple (LCM) of two numbers is a common problem. These concepts are essential in number theory, and they have practical applications in various fields, including cryptography, computer algorithms, and more. In this blog post, we will walk through a simple Java program that calculates both the HCF and LCM of two numbers.

Understanding HCF and LCM

HCF (Highest Common Factor), also known as GCD (Greatest Common Divisor), is the largest number that divides two or more numbers without leaving a remainder. For example, the HCF of 15 and 30 is 15.

LCM (Least Common Multiple) is the smallest number that is a multiple of two or more numbers. For example, the LCM of 15 and 30 is 30.

Java Program to Find HCF and LCM

Let's dive into the Java code that calculates both the HCF and LCM of two numbers.
        
        import java.io.*;
        import java.util.Scanner;

        public class hcflcm {
            public static void main(String args[]) {
                int a, b, x, y, t, hcf, lcm;
                Scanner scan = new Scanner(System.in);

                System.out.print("Enter 2 numbers: ");
                x = scan.nextInt();
                y = scan.nextInt();

                a = x;
                b = y;

                // Calculate HCF using the Euclidean algorithm
                while (b != 0) {
                    t = b;
                    b = a % b;
                    a = t;
                }

                hcf = a;
                lcm = (x * y) / hcf;

                // Output the results
                System.out.println("HCF = " + hcf);
                System.out.println("LCM = " + lcm);
            }
        }
              
        
        


Output


        Enter 2 numbers: 15
        30
        HCF = 15
        LCM = 30
        



Explanation

1. User Input:
    The program begins by prompting the user to enter two numbers. These numbers are stored in the variables x and y.
2. Copying Values:
    The original values of x and y are copied into a and b to preserve the original numbers for later use in calculating the LCM.
3. Calculating HCF:
    The HCF is calculated using the Euclidean algorithm. This algorithm works by repeatedly replacing the larger number with its remainder when divided by the smaller number until the remainder is zero. The last non-zero remainder is the HCF.
4. Calculating LCM:
    The LCM is then calculated using the relationship between HCF and LCM: 
5. Output:
    The program then prints the calculated HCF and LCM.


Conclusion

     This Java program provides a simple and efficient way to calculate both the HCF and LCM of two numbers using the Euclidean algorithm. Understanding how to find these values is essential in various mathematical and programming contexts. The program is easy to understand and can be a great starting point for those learning about number theory in programming.

Feel free to modify the program to handle more complex scenarios, such as finding the HCF and LCM of more than two numbers. Happy coding!

Post a Comment

0 Comments