Find LCM of Two Given Numbers ~ IN JAVA
PROGRAME THAT CALULATED L.C.M OF TWO GIVEN NUMBER IN JAVA
import java.util.Scanner;
public class LeastCommonMultiple {
public static void main(String[] args) {
int numberOne = input("Enter your first number: "), numberTwo = input("Enter your first number: ");
System.out.println("The " + numberOne + " And " + numberTwo + " L.C.M Are " + lcm(numberOne,numberTwo));
}
// These function use for taking input from user
private static int input(String command) {
Scanner input = new Scanner(System.in);
System.out.print(command);
return input.nextInt();
}
// These function are use for lcm
private static int lcm(int numberOne, int numberTwo){
int i = 1, result = 1;
// Checks if i is factor of both integers
while (i <= numberOne && i <= numberTwo){
if (numberOne % i == 0 && numberTwo % i == 0){
result = i;
}
i++;
}
return (numberOne * numberTwo) / result;
}
}
Comments
Post a Comment