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:
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (remainder) | a % b |
Addition (+)
Definition: The
+operator adds two numbers together.Syntax:
Example of Addition
Output:
Addition works for integers, floats, and doubles.
Example with float:
Subtraction (-)
Definition: The
-operator subtracts one number from another.Syntax:
Example of Subtraction
Output:
Subtraction can also result in negative numbers.
Multiplication (*)
Definition: The
*operator multiplies two numbers.Syntax:
Example of Multiplication
Output:
Multiplication is commutative, meaning
a * b = b * a.
Division (/)
Definition: The
/operator divides one number by another.Syntax:
Integer Division
When both operands are integers, the division result is integer only (decimal part is discarded).
Output:
Decimal part is ignored because both numbers are integers.
Float Division
To get accurate division, use
floatordouble.
Output:
Modulus (%)
Definition: The
%operator gives the remainder of integer division.Syntax:
Example of Modulus
Output:
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:
()– Parentheses (highest precedence)*,/,%– Multiplication, Division, Modulus+,-– Addition, Subtraction
Associativity: Most arithmetic operators are left-to-right, except unary operators.
Examples of Operator Precedence
Without Parentheses
Output:
5 * 2 = 10first, then10 + 10 = 20.
With Parentheses
Output:
Parentheses change the order:
(10 + 5) = 15first, then15 * 2 = 30.
Complex Expressions
You can combine all operators in one expression.
Step by Step Calculation:
b * c = 10 * 5 = 5050 / d = 50 / 2 = 25a + 25 = 20 + 25 = 4545 - 3 = 42
Output:
Operator precedence ensures accurate results without manually calculating step by step.
Tips for Using Operator Precedence
Use parentheses for clarity, even if not required.
Avoid ambiguity in complex expressions.
Remember integer division truncates decimals.
Modulus always applies after division/multiplication in precedence.
Quick Table of Precedence
| Operator | Precedence | Associativity |
|---|---|---|
() | 1 | Left-to-right |
* / % | 2 | Left-to-right |
+ - | 3 | Left-to-right |
Rule: Higher precedence operators are executed before lower precedence ones.
Practice Problems
Calculate the result of
10 + 6 / 2 * 3 - 1.Predict the output of
(20 - 5) * 2 + 10 / 5.Write a program to find sum, difference, product, quotient, and remainder of two numbers.
Use float division to display decimal results.
These exercises help reinforce arithmetic operations and precedence understanding.