write a program in java for taking out hcf and lcm of two numbers using for loop only. Please tell
Answers
Answered by
8
//Program to find GCD(HCF) and LCM
import java.util.*;
class LCM_GCD
{
public static void main(String args [ ])
{
Scanner sc = new Scanner(System.in);
int a,b,p,i;
double lcm,gcd;
System.out.print("Enter 2 no.s");
a = sc.nextInt();
b = sc.nextInt();
p = a*b;
for(i=0;i<=p;i++)
{
if(a%i==0 && b%i==0)
gcd = (double)i;
}
lcm = (double) gcd/p;
System.out.println("HCF:\t" +gcd+"\nLCM:\t" +lcm);
}
}
Hope this helps you.
Similar questions