0

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);
Ben Myers
  • 1,173
  • 1
  • 8
  • 25
tin
  • 68
  • 7
  • If you use `&&`, you're requiring that _all_ of the numbers are different. If you use `||` you're requiring that any of the numbers are different. – khelwood Oct 18 '20 at 19:45
  • `a && b && c` means *all* conditions must be true. In your case this means all numbers must be different. `a || b|| c` means *at least one* condition must be true. In your case this means at least two of the three numbers must be different. So for the first case, your program stops when two of the numbers become equal, in the second case it only stops when all three are equal – derpirscher Oct 18 '20 at 19:53
  • See [De Morgan's rules explained](https://stackoverflow.com/q/2168603/3890632) – khelwood Oct 18 '20 at 19:53
  • @derpirscher * in the second case it only stops when all three are equal* , but why ive connected the statements with "OR" , doesnt that mean that one of the statements should turn true for the process to stop ? – tin Oct 20 '20 at 23:32
  • No. The loop continues while one of the conditions is true – derpirscher Oct 21 '20 at 07:10

1 Answers1

0

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.

The loop will stop when any of the two numbers are the same. See the explanation given below:

Assuming num1 = num2 = 5 and num3 = 10,

5 != 10 && num1!=num2 && num3!=num2 => true && 5!=5 && num3!=num2 => true && false && num3!=num2 => false && num3!=num2 => false

Assuming num1 = num3 = 5 and num2 = 10,

5 != 5 && num1!=num2 && num3!=num2 => false && num1!=num2 && num3!=num2 => false

Assuming num1 = 10 and num2 = num3 = 5,

10 != 5 && num1!=num2 && num3!=num2 => true && 10!=5 && num3!=num2 => true && true && num3!=num2 => true && 5!=5 => true && false => false

Assuming num1 = num2 = num3 = 5,

5 != 5 && num1!=num2 && num3!=num2 => false && num1!=num2 && num3!=num2 => false

Note that when more than two conditions are joined with && the evaluation stops as soon as any of the condition (from left to right) evaluates to false.

In an attempt to guess, I changed && to || as a last resort, but still do not understand its logic.

In this case, the loop will stop only when all the numbers are the same. See the explanation given below:

Assuming num1 = num2 = 5 and num3 = 10,

5 != 10 || num1!=num2 || num3!=num2 => true || num1!=num2 || num3!=num2 => true

Assuming num1 = num3 = 5 and num2 = 10,

5 != 5 || num1!=num2 || num3!=num2 => false || 5!=10 || num3!=num2 => false || true || num3!=num2 => true || num3!=num2  => true

Assuming num1 = 10 and num2 = num3 = 5,

10 != 5 || num1!=num2 || num3!=num2 => true || num1!=num2 || num3!=num2 => true

Assuming num1 = num2 = num3 = 5,

5 != 5 || num1!=num2 || num3!=num2 => false || 5!=5 || num3!=num2 => false || false || 5!=5 => false || false => false

Note that when more than two conditions are joined with || the evaluation stops as soon as any of the condition (from left to right) evaluates to true.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Sorry but I dont really get what you are trying to say – tin Oct 20 '20 at 23:30
  • @tin - You have mentioned, `The program works as intended.` and you wanted to understand how the `&&` and `||` operators work when multiple of them are used in a single expression. I wrote this answer to explain that. I'm sure you can understand every bit of it if you read the answer wholeheartedly. Feel free to comment in case of any doubt. – Arvind Kumar Avinash Oct 21 '20 at 06:57