Arithmetic operators are the foundation of mathematical operations in C++ programming. They allow you to perform basic calculations like addition, subtraction, multiplication, division, and modulus. Understanding arithmetic operators is essential for creating programs that involve calculations, formulas, or numeric processing.

Overview of Arithmetic Operators

In C++, arithmetic operators are symbols that perform mathematical operations on numeric data types such as int, float, double, and long.

The common arithmetic operators in C++ include:

OperatorMeaningExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (remainder)a % b

Addition (+)

  • Definition: The + operator adds two numbers together.

  • Syntax:

 
result = operand1 + operand2;

Example of Addition

 
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 5;
int sum = a + b;
cout << "Sum: " << sum << endl;
return 0;
}

Output:

 
Sum: 15
  • Addition works for integers, floats, and doubles.

  • Example with float:

 
float x = 5.5, y = 4.5;
cout << "Sum: " << x + y;
// Output: 10.0

Subtraction (-)

  • Definition: The - operator subtracts one number from another.

  • Syntax:

 
result = operand1 - operand2;

Example of Subtraction

 
#include <iostream>
using namespace std;
int main() {
int a = 15, b = 7;
int difference = a - b;
cout << "Difference: " << difference << endl;
return 0;
}

Output:

 
Difference: 8
  • Subtraction can also result in negative numbers.

Multiplication (*)

  • Definition: The * operator multiplies two numbers.

  • Syntax:

 
result = operand1 * operand2;

Example of Multiplication

 
#include <iostream>
using namespace std;
int main() {
int a = 6, b = 4;
int product = a * b;
cout << "Product: " << product << endl;
return 0;
}

Output:

 
Product: 24
  • Multiplication is commutative, meaning a * b = b * a.

Division (/)

  • Definition: The / operator divides one number by another.

  • Syntax:

 
result = operand1 / operand2;

Integer Division

  • When both operands are integers, the division result is integer only (decimal part is discarded).

 
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 3;
int quotient = a / b;
cout << "Quotient: " << quotient << endl;
return 0;
}

Output:

 
Quotient: 3
  • Decimal part is ignored because both numbers are integers.

Float Division

  • To get accurate division, use float or double.

 
#include <iostream>
using namespace std;
int main() {
double a = 10.0, b = 3.0;
double quotient = a / b;
cout << "Quotient: " << quotient << endl;
return 0;
}

Output:

 
Quotient: 3.33333

Modulus (%)

  • Definition: The % operator gives the remainder of integer division.

  • Syntax:

 
remainder = operand1 % operand2;

 Example of Modulus

 
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 3;
int remainder = a % b;
cout << "Remainder: " << remainder << endl;
return 0;
}

Output:

 
Remainder: 1
  • Note: Modulus works only with integers in C++.

Operator Precedence

Operator precedence determines the order in which arithmetic operations are performed in an expression. In C++, some operators are evaluated before others unless parentheses are used.

 Precedence Rules

The order of precedence for arithmetic operators in C++ is:

  1. () – Parentheses (highest precedence)

  2. *, /, % – Multiplication, Division, Modulus

  3. +, - – Addition, Subtraction

  • Associativity: Most arithmetic operators are left-to-right, except unary operators.

 Examples of Operator Precedence

 Without Parentheses

 
#include <iostream>
using namespace std;
int main() {
int result = 10 + 5 * 2; // Multiplication first
cout << "Result: " << result << endl;
return 0;
}

Output:

 
Result: 20
  • 5 * 2 = 10 first, then 10 + 10 = 20.

With Parentheses

 
#include <iostream>
using namespace std;
int main() {
int result = (10 + 5) * 2; // Parentheses first
cout << "Result: " << result << endl;
return 0;
}

Output:

 
Result: 30
  • Parentheses change the order: (10 + 5) = 15 first, then 15 * 2 = 30.

Complex Expressions

  • You can combine all operators in one expression.

 
#include <iostream>
using namespace std;
int main() {
int a = 20, b = 10, c = 5, d = 2;
int result = a + b * c / d - 3;
cout << "Result: " << result << endl;
return 0;
}

Step by Step Calculation:

  1. b * c = 10 * 5 = 50

  2. 50 / d = 50 / 2 = 25

  3. a + 25 = 20 + 25 = 45

  4. 45 - 3 = 42

Output:

 
Result: 42
  • Operator precedence ensures accurate results without manually calculating step by step.

Tips for Using Operator Precedence

  1. Use parentheses for clarity, even if not required.

  2. Avoid ambiguity in complex expressions.

  3. Remember integer division truncates decimals.

  4. Modulus always applies after division/multiplication in precedence.

Quick Table of Precedence

OperatorPrecedenceAssociativity
()1Left-to-right
* / %2Left-to-right
+ -3Left-to-right
  • Rule: Higher precedence operators are executed before lower precedence ones.

Practice Problems

  1. Calculate the result of 10 + 6 / 2 * 3 - 1.

  2. Predict the output of (20 - 5) * 2 + 10 / 5.

  3. Write a program to find sum, difference, product, quotient, and remainder of two numbers.

  4. Use float division to display decimal results.

  • These exercises help reinforce arithmetic operations and precedence understanding.