← Back to C++ Course | Chapter 3: Operators | Lesson 2 of 9

C++ Arithmetic Operators

Arithmetic operators are the math buttons on a calculator: plus, minus, times, divide, and remainder. C++ uses them to calculate with numbers.
Syntax
cpp
result = operand1 + operand2;   // also - * / %

Addition and Subtraction

Addition (+) and subtraction (-) work exactly as expected on both integers and floating-point numbers, combining or offsetting two values.

These are the building blocks nearly every numeric calculation in a program starts from, whether it's totaling a cart or computing a difference in coordinates.

Example: Addition and Subtraction

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare sum and set it to 5 + 3
	int sum = 5 + 3;
	// Declare diff and set it to 5 - 3
	int diff = 5 - 3;
	// Print to the console with cout
	std::cout << sum << " " << diff << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Multiplication and Division

Multiplication (*) and division (/) behave differently depending on the operand types: dividing two ints performs integer division, silently discarding any remainder, so 7 / 2 evaluates to 3, not 3.5.

To get a fractional result, at least one operand needs to be a floating-point type.

Example: Multiplication and Division

cpp
#include <iostream>

int main() {
	std::cout << 7 / 2 << std::endl;   // integer division: 3
	std::cout << 7.0 / 2 << std::endl; // 3.5
	return 0;
}

Modulo Operator

The modulo operator (%) returns only the remainder left over from integer division and requires both operands to be integers — for example, 10 % 3 evaluates to 1.

A very common practical use is checking even/odd with n % 2 == 0, or wrapping a value into a fixed range like cycling through array indices.

Example: Modulo Operator

cpp
#include <iostream>

int main() {
	std::cout << 10 % 3 << std::endl; // remainder: 1
	int n = 8;
	std::cout << (n % 2 == 0) << std::endl; // even check
	return 0;
}

Increment and Decrement Operators

++ and -- adjust a variable by exactly 1, and their position matters: prefix (++x) increments the variable before its new value is used in the surrounding expression, while postfix (x++) uses the original value first and increments afterward.

This distinction matters most inside larger expressions like arr[i++], where the timing of the increment changes which element gets accessed.

Example: Increment and Decrement Operators

cpp
#include <iostream>

int main() {
	int x = 5;
	std::cout << ++x << std::endl; // prefix: increments first, prints 6
	std::cout << x++ << std::endl; // postfix: prints 6, then becomes 7
	std::cout << x << std::endl;
	return 0;
}

Compound Assignment Operators

Compound assignment operators like +=, -=, *=, and /= fold an operation and an assignment into one step, so total += price; is shorthand for total = total + price;.

Beyond saving keystrokes, this shorthand also reads more clearly as 'update this variable' rather than 'reassign this variable to a new expression involving itself'.

Example: Compound Assignment Operators

cpp
#include <iostream>

int main() {
	int total = 100;
	int price = 20;
	total += price; // shorthand for total = total + price
	std::cout << total << std::endl;
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Dividing two ints like 7 / 2 and expecting 3.5, when the result is 3 because both operands are integers.
  2. Using % on doubles such as 5.5 % 2, which does not compile; % only works on integers.
  3. Dividing by zero with integers, which is undefined behavior and often crashes.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.