Back to questions
JuniorTheoryCommon
What does the program print?
#include <iostream>
int main() {
std::cout << 7 / 2 << ' ' << -7 % 3 << '\n';
}3 -1
- Expecting
3.5: dividing oneintby another drops the fraction, so7 / 2is3. - Expecting a non-negative remainder, as in math: in C++ the remainder takes the sign of the dividend, so
-7 % 3is-1.
- How would you change the expression so that the division prints
3.5? - What does
7 % -3evaluate to, and why?