Computer Science, asked by hetu3, 1 year ago

Pls write a java program to find hcf and Lcm of 2 numbers

Answers

Answered by Rachit127
46
find the HCF and LCM of two number in Java Programming, you have to ask to the user to enter the two number, to find the HCF and LCM of the given two number to display the value of the HCF and LCM of the two numbers on the output screen as shown in the following program.
import java.util.Scanner;

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

System.out.print("Enter Two Number : ");
x = scan.nextInt();
y = scan.nextInt();

a = x;
b = y;

while(b != 0)
{
t = b;
b = a%b;
a = t;
}

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

System.out.print("HCF = " +hcf);
System.out.print("\nLCM = " +lcm);
}
}
if it is helpful so so select it as the brainliest and click on thnx
Answered by UnknownDude
12
import java.util.*;
class HCF_LCM
{
public static void main()
{
Scanner in=new Scanner(System.in);
int a,b,hcf=0,lcm=1,min=0;
System.out.println("Enter two numbers");
a=in.nextInt();
b=in.nextInt();
if(a<b)
min=a;
else
min=b;
for(int i=1;i<=min;i++)
{
if(a%i==0&&b&i==0)
hcf=i;
}
lcm=(a*b)/hcf; // bcoz product of numbers = product of hcf and lcm
System.out.println("HCF:"+hcf);
System.out.println("LCM:"+lcm);
}
}
}
Similar questions