Skip to content

Instantly share code, notes, and snippets.

@mehtaparitosh
Last active August 24, 2017 05:47
Show Gist options
  • Save mehtaparitosh/36a1250f85a3fe0eba43d69478d2a6c8 to your computer and use it in GitHub Desktop.
Save mehtaparitosh/36a1250f85a3fe0eba43d69478d2a6c8 to your computer and use it in GitHub Desktop.
An efficient way to find HCF and LCM
import java.util.Scanner
public class JavaProgram
{
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment