Arithmetic operators are used to perform mathematical calculations in C++. They can be used with variables, constants, and expressions to perform operations such as addition, subtraction, multiplication, division, and finding a remainder.
Arithmetic operators are symbols used to perform mathematical operations on values.
int a = 10;
int b = 5;
int result = a + b;
Here, + is an arithmetic operator.
| Operator | Name | Example |
|---|---|---|
| + | Addition | a + b |
| - | Subtraction | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulus | a % b |
The + operator adds two values.
int a = 10;
int b = 20;
int sum = a + b;
std::cout << sum;
The result is 30.
Two or more variables can be added together.
int first = 25;
int second = 15;
int total = first + second;
std::cout << total;
The output is 40.
The addition operator can also be used with floating-point values.
double price1 = 99.50;
double price2 = 50.25;
double total = price1 + price2;
std::cout << total;
The result is 149.75.
The - operator subtracts the right operand from the left operand.
int a = 50;
int b = 20;
int result = a - b;
std::cout << result;
The result is 30.
If the second value is larger, subtraction can produce a negative result.
int a = 20;
int b = 50;
int result = a - b;
std::cout << result;
The result is -30.
The * operator multiplies two values.
int price = 100;
int quantity = 5;
int total = price * quantity;
std::cout << total;
The result is 500.
double price = 99.50;
double quantity = 2;
double total = price * quantity;
std::cout << total;
The result is 199.
The / operator is used to divide one value by another.
int total = 100;
int students = 5;
int result = total / students;
std::cout << result;
The result is 20.
When both operands are integers, division performs integer division. The fractional part is discarded.
int a = 5;
int b = 2;
int result = a / b;
The result is 2, not 2.5.
If at least one operand is a floating-point value, the division can produce a floating-point result.
double a = 5;
double b = 2;
double result = a / b;
std::cout << result;
The result is 2.5.
You can explicitly convert an integer before division.
int a = 5;
int b = 2;
double result =
static_cast<double>(a) / b;
std::cout << result;
The result is 2.5.
The % operator returns the remainder after integer division.
int result = 17 % 5;
std::cout << result;
The result is 2.
The modulus operator is commonly used to check whether a number is even or odd.
int number = 10;
if (number % 2 == 0) {
std::cout << "Even";
} else {
std::cout << "Odd";
}
If the remainder is zero, the number is even.
| Expression | Result |
|---|---|
10 % 3 |
1 |
15 % 4 |
3 |
20 % 5 |
0 |
25 % 6 |
1 |
The unary + operator is applied to a single operand and preserves its value.
int number = 10;
int result = +number;
The value of result is 10.
The unary - operator changes the sign of its operand.
int number = 10;
int result = -number;
std::cout << result;
The result is -10.
Multiple arithmetic operators can be combined in one expression.
int a = 10;
int b = 5;
int c = 2;
int result = a + b * c;
Multiplication is performed before addition according to operator precedence.
Parentheses can be used when you want a particular part of an expression to be evaluated first.
int a = 10;
int b = 5;
int c = 2;
int result = (a + b) * c;
Here, a + b is evaluated before multiplication.
int a = 20;
int b = 10;
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
All five basic arithmetic operators are demonstrated here.
#include <iostream>
int main() {
int a, b;
std::cout << "Enter first number: ";
std::cin >> a;
std::cout << "Enter second number: ";
std::cin >> b;
std::cout << "Sum = " << a + b;
return 0;
}
The values entered by the user are used in an arithmetic operation.
#include <iostream>
int main() {
double a, b;
std::cout << "Enter first number: ";
std::cin >> a;
std::cout << "Enter second number: ";
std::cin >> b;
std::cout << "Addition: " << a + b << std::endl;
std::cout << "Subtraction: " << a - b << std::endl;
std::cout << "Multiplication: " << a * b << std::endl;
if (b != 0) {
std::cout << "Division: " << a / b << std::endl;
}
return 0;
}
A program should not attempt division by zero.
int a = 10;
int b = 0;
// a / b; // Do not do this
Always validate a divisor before performing division when it may be zero.
| Expression | Typical Result Type |
|---|---|
10 + 5 |
int |
10.5 + 5.0 |
double |
10 / 3 |
int |
10.0 / 3 |
double |
10 % 3 |
int |
The actual result type follows C++'s usual arithmetic conversions.
Arithmetic operations can also be combined with assignment.
int number = 10;
number += 5;
number -= 2;
number *= 2;
number /= 4;
Each statement performs an arithmetic operation and assigns the result back to number.
#include <iostream>
int main() {
int english = 80;
int maths = 90;
int science = 85;
int total = english + maths + science;
double average =
static_cast<double>(total) / 3;
std::cout << "Total = " << total << std::endl;
std::cout << "Average = " << average;
return 0;
}
The total uses addition, while the average uses division with an explicit conversion to double.
#include <iostream>
int main() {
int a = 25;
int b = 4;
std::cout << "Addition: "
<< a + b << std::endl;
std::cout << "Subtraction: "
<< a - b << std::endl;
std::cout << "Multiplication: "
<< a * b << std::endl;
std::cout << "Integer Division: "
<< a / b << std::endl;
std::cout << "Remainder: "
<< a % b << std::endl;
std::cout << "Decimal Division: "
<< static_cast<double>(a) / b
<< std::endl;
return 0;
}
| Operator | Name | Example | Purpose |
|---|---|---|---|
| + | Addition | 10 + 5 |
Add values |
| - | Subtraction | 10 - 5 |
Subtract values |
| * | Multiplication | 10 * 5 |
Multiply values |
| / | Division | 10 / 5 |
Divide values |
| % | Modulus | 10 % 3 |
Find remainder |
Question: Which arithmetic operator is used to find the remainder of an integer division?