Java Program to find HCF and LCM

In this Java programming tutorial, we are providing java source code to calculate the HCF and LCM of two given numbers entered by the user.

Source Code of Java Program to find HCF and LCM

import java.util.*;
class HCFNLCM {
        public static void main(String Args[]) {
                System.out.println("Enter 2 numbers");
                Scanner sc = new Scanner(System.in);
                int m = sc.nextInt();
                int n = sc.nextInt();
                int h = 1;
                int p = m * n;
                for (int i = 2; i < p; i++) {
                        if ((m % i == 0) && (n % i == 0)) {
                                h = i;
                        }
                }
                int l = p / h;
                System.out.println("HCF=" + h + " and LCM=" + l);

        }
}

Output of Java Program for finding HCF and LCM

0 comments:

Post a Comment