My java program for finding the LCM of two numbers is giving wrong output for (13, 12). Please check what is the error. I think the error is with the loop I cannot figure out what to do next help me please.
import java.util.*;
public class Bonjour{
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.println("Enter 2 numbers: ");
int num1 = scan.nextInt();
int num2 = scan.nextInt();
if(num1 % num2 == 0)
{
System.out.println("The LCM = "+num1);
}
else if(num2 % num1 == 0)
{
System.out.println("The LCM = "+num2);
}
else if(num2 % num1 != 0) // 2, 3 = 6;
{
for(int i = 1;i <= 100;i++){
if(((num1 * i) % num2) == 0){
System.out.println("The LCM = "+(num1 * i));
break;
}
}
}
else{
System.out.println("The LCM = "+(num1*num2));
}
scan.close();
}
}
The output of the program when I input(12, 13) is blank. But it is working for other inputs. Sample output for '12' and '13'.
Enter 2 numbers:
12
13
//No output??
Sample output for '3' and '2':
Enter 2 numbers:
3
2
The LCM = 6
Aucun commentaire:
Enregistrer un commentaire