The AND
(&&
) condition evaluates as OR
(||
) in this code. For ex., when user inputs numbers 6 and 7, the output is 12 but when the I replace &&
with ||
the output is 42.
#include<stdio.h>
int main()
{
int a,b,max,lcm;
printf("Enter 2 numbers: ");
scanf("%d %d", &a, &b);
if(a>b)
{
max=a;
}
else
{
max=b;
}
do
{
max++;
} while(max%a!=0 && max%b!=0);
lcm=max;
printf("LCM of %d and %d is %d\n", a,b,lcm);
return 0;
}
No, the &&
condition is working as an and
condition, just as it should. When you input 6
and 7
, max%a
evaluates to 0
when max
is 12
. At that point max%a != 0
evaluates to false
(false && true == false
), and max%a != 0 && max%b != 0
evaluates to false
, and your loop exits. However, max%a != 0 || max%b != 0
evaluates to true
(max%b
is 5
for a max
of 12
and b
of 7
, false || true == true
), so the loop continues.