20. Create a class with the following methods:
i) int HCF(int a, int b), which returns the highest common factor (HCF) among the two integers a and b and return it.
ii) int LCM(int , int b), which returns the least common multiple (LCM) among the two integers a and b and return it.
Answers
Answered by
2
Explanation:
i)
import java.jtil.*;
class HCF
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,a,b,h=0;
System.out.println(“Enter 2 integers:”);
a=sc.nextInt();
b=sc.nextInt();
for(i=a;i>=1;i--)
{
if(a%i==0 && b%i==0)
{
h=i;
break;
}
}
System.out.println(“H.C.F.=”+h);
}
}
Enter two numbers it will find the HCF of it
ii)
import java.util.";
class LCM
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,a,b,l=0;
System.out.println("Enter 2 integer:"); a=sc.nextInt(); b=sc.nextInt();
for(i=a;i<=a*b;i++)
{
if(i%a== 0 && i%b==0)
{
break;
}
}
System.out.println("L.C.M.="+1);
}
}
it will find the LCM of two numbers
I have written the correct program no error
Similar questions