Note: The program works as intended.
I made a program in Java to calculate the LCM of 3 numbers. Initially, I programmed my with the condition while( num1 != num3 && num1!=num2 && num3!=num2)
. My initial reasoning was that the loop should stop when all of the numbers were the same.
In an attempt to guess, I changed &&
to ||
as a last resort, but still do not understand its logic.
Scanner input = new Scanner(System.in);
System.out.print("Type your 1st number :");
int num1=input.nextInt();
System.out.print("Type your 2nd number :");
int num2=input.nextInt();
System.out.print("Type your 3rd number :");
int num3=input.nextInt();
int n=num1;
int m=num2;
int b=num3;
while( num1 != num3 || num1!=num2 || num3!=num2){
if (num2 > num1 || num3 > num1)
num1 += n;
if (num1 > num2)
num2 += m;
if (num2 > num3)
num3 += b;
}
System.out.println("LCM of "+"'"+n+"'"+" and "+"'"+m+"'"+" and "+"'"+b+"'"+" is "+num3);