class Example{
public static void main(String[] args) {
int a=7 % 10 / 2 * 2;
System.out.println(a);
}
}
I was expecting 7 but the answer is 6. What is the reason for this?
>Solution :
class Example {
public static void main(String[] args) {
int a = 7 % 10 / 2 * 2;
// The expression 7 % 10 calculates the remainder of 7 divided by 10, which is 7.
// The expression 7 % 10 / 2 calculates the result of dividing 7 by 2, which is 3 (the remainder is discarded and quotient is an integer value).
// The expression 7 % 10 / 2 * 2 multiplies the result of the previous division by 2, resulting in 6.
System.out.println(a); // Prints 6 to the console.
}
}