:::: MENU ::::
  • Suitable for all screen sizes

  • Easy to Customize

  • Customizable fonts.

Saturday, December 19, 2015



HCF and LCM of a number:

import java.util.*;

class hcfandlcm

{

    public static void main()

    {

        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);

    }

}

Thank You and make sure to Like and Comment.

3 comments:

  1. Wouldn't the algo work faster with
    p=Math.min(m,n);

    ReplyDelete
  2. Nice article for find hcf for two number .one more way to find hfc and lcm visit HCF and LCM of two number

    ReplyDelete

A call-to-action text Contact us