C++ Arithmetic Operators

Arithmetic operators let you do everyday math in C++: adding, subtracting, multiplying, and dividing. There is also a special operator called modulus that gives you the remainder of a division. These are the operators you will reach for most often.

The five arithmetic operators

C++ gives you five core arithmetic operators. Four of them match the math you already know, and the fifth, modulus, is probably new but very useful once you understand it.

Arithmetic operators

OperatorNameExampleResult
+Addition7 + 29
-Subtraction7 - 25
*Multiplication7 * 214
/Division7 / 23 (integer division)
%Modulus7 % 21 (remainder)

Basic arithmetic

#include <iostream>
using namespace std;

int main() {
  int x = 12;
  int y = 5;

  cout << "Sum: " << x + y << "\n";
  cout << "Difference: " << x - y << "\n";
  cout << "Product: " << x * y << "\n";
  cout << "Quotient: " << x / y << "\n";
  cout << "Remainder: " << x % y << "\n";
  return 0;
}

Watch out for division. When both operands are integers, C++ throws away the fractional part and keeps only the whole number. So 7 / 2 is 3, not 3.5. If you want a decimal answer, at least one of the values must be a floating-point type such as double.

Integer versus decimal division

#include <iostream>
using namespace std;

int main() {
  int a = 7, b = 2;
  double c = 7.0, d = 2.0;

  cout << "Integer division: " << a / b << "\n";   // 3
  cout << "Decimal division: " << c / d << "\n";   // 3.5
  return 0;
}
  • Use % only with integers, never with doubles
  • Modulus is great for checking if a number is even: n % 2 == 0
  • Multiplication and division run before addition and subtraction
  • Dividing by zero causes undefined behavior, so always guard against it
Note: C++ also has increment (++) and decrement (--) operators that add or subtract 1 from a variable. You will see these constantly in loops, for example i++ to move to the next step.