Last active
August 24, 2017 05:47
-
-
Save mehtaparitosh/36a1250f85a3fe0eba43d69478d2a6c8 to your computer and use it in GitHub Desktop.
An efficient way to find HCF and LCM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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