LCM And GCD Of Two Numbers In Java
Chapter:
Math Class
Last Updated:
14-05-2017 18:28:07 UTC
Program:
/* ............... START ............... */
import java.util.Scanner;
public class JavaLCMGCD {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the two numbers: ");
int number1 = input.nextInt();
int number2 = input.nextInt();
System.out.println("The GCD of two numbers is: " + gcd(number1, number2));
System.out.println("The LCM of two numbers is: " + lcm(number1, number2));
input.close();
}
// Using Eucid algorithm for calculating gcd
public static int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
public static int lcm(int a, int b) {
return a * b / (gcd(a, b));
}
}
/* ............... END ............... */
Output
Enter the two numbers:
2
3
The GCD of two numbers is: 1
The LCM of two numbers is: 6
Tags
LCM And GCD Of Two Numbers, Java, Math