Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why the answer for this is 6?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.
    } 
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading