Assuming standard headers and using namespace std, what does this C++ code print? int a = 7, b = 2; cout << a / b << ” ” << a % b << ” ” << 7.0 / b;
Correct answer C. 3 1 3.5
Explanation
When both operands of / are int, C++ performs integer division and discards the fractional part, so 7 / 2 is 3, and 7 % 2 gives the remainder 1. In 7.0 / b the left operand is a double, so b is converted to double and the result is 3.5. Mixing one floating-point operand into an arithmetic expression is enough to switch it to floating-point arithmetic.
Answer checked How we write and check questions
Discussion