#include using namespace std; /* Illustrates the behavior of a/b and a%b with negative integer arguments a and b. The behavior is system dependent: different platforms may give different answers, when one or more of the arguments a and b is negative. The answers must always satisfy the following equation: a == (a/b)*b + (a%b). When both a and b are positive, the values of a/b and a%b are uniquely determined, because 0 <= a%b < b is required. But when either a or b is negative, many systems allow a%b to be negative, even though the standard mathematical convention requires the remainder in integer division to be nonnegative in all cases. */ int main(){ cout << " -4 / 7 = " << ( -4 / 7 ) << endl; cout << " -4 % 7 = " << ( -4 % 7 ) << endl << endl; cout << " (-4) / 7 = " << ( (-4) / 7 ) << endl; // Redundant. cout << " (-4) % 7 = " << ( (-4) % 7 ) << endl << endl; cout << " 4 / -7 = " << ( 4 / -7 ) << endl; cout << " 4 % -7 = " << ( 4 % -7 ) << endl << endl; cout << " -4 / -7 = " << ( -4 / -7 ) << endl; cout << " -4 % -7 = " << ( -4 % -7 ) << endl << endl; return 0; } /* Output on kiwi.cs.ucla.edu: -4 / 7 = 0 -4 % 7 = -4 (-4) / 7 = 0 (-4) % 7 = -4 4 / -7 = 0 4 % -7 = 4 -4 / -7 = 0 -4 % -7 = -4 */