Arithmetic forms the backbone of nearly all computer programs, from calculating totals and averages to processing physics simulations and financial models. In C++, arithmetic is performed using operators—symbols that instruct the compiler to carry out specific mathematical actions. These operators interact with operands, or values the operators act on, to produce results like sums, products, or quotients.
Most arithmetic in C++ involves binary operators, which are operators that act on exactly two operands. For example, in the expression 10 + 3
, the +
symbol is a binary operator because it operates on two values: 10
and 3
. Understanding how these binary operators behave—especially when working with different data types like integers and floating-point numbers—is essential for writing accurate, reliable programs.
In this section, we’ll explore how C++ handles arithmetic with integers and floating-point values, how mixed-type expressions behave, and how operator precedence affects complex equations. Whether you’re building a calculator or laying the foundation for more advanced logic, mastering C++ arithmetic is a critical step.
C++ comes with built in standard arithmetic operators and they can be used without including any additional libraries:
Description | Operator | Example |
---|---|---|
addition | + | 10 + 3 = 13 |
subtraction | - | 10 - 13 = -3 |
multiplication | * | 10 * 3 = 30 |
division | / | 10 / 3 = 3 |
modulus | % | 10 % 3 = 1 |
From the table above it can be seen that addition, subtraction, and multiplication work as expected, but division of 10 / 3
results in 3
. This is because both 10
and 3
are integers, thus they can only give an integer result of 3
rather than 3.33...
. The .33
(also called the mantissa) just gets chopped off because the remainder is left alone when performing the division. Since the result is integer nothing gets done with the mantissa so it is essentially chopped off. For this reason even if the division were 11 / 3
the answer would still be 3
and would not round up to 4
even though in traditional math the answer would be 3.66...
.
To get the remainder of division when using integer use the modulus operator %
. This will return the remainder of the division performed on the two operands:
Modulus Operation | Result |
---|---|
9 % 3 | 0 |
10 % 3 | 1 |
11 % 3 | 2 |
12 % 3 | 0 |
The value returned by modulus will always be in the range 0 ≤ value ≤ denominator - 1
.
Each of the integer arithmetic operators can be used in a program in the following way:
#include <iostream> using namespace std; int main() { // compute arithmetic and output results cout << “10 + 3 = “ << 10 + 3 << endl << “10 - 13 = “ << 10 - 13 << endl << “10 * 3 = “ << 10 * 3 << endl << “10 / 3 = “ << 10 / 3 << endl << “10 % 3 = “ << 10 % 3 << endl; return 0; }
Which outputs:
example % ./a.out 10 + 3 = 13 10 - 13 = -3 10 * 3 = 30 10 / 3 = 3 10 % 3 = 1
To save values into variables so they can be used in later operations:
#include <iostream> using namespace std; int main() { // compute arithmetic int lhs = 10, rhs = 3; int add = lhs + rhs; int sub = lhs - add; // output results cout << lhs << “ + “ << rhs << “ = “ << add << endl << lhs << “ - “ << add << “ = “ << sub << endl; return 0; }
Which outputs:
example % ./a.out 10 + 3 = 13 10 - 13 = -3
This can be combined with input to allow for the user to input the values in the arithmetic:
#include <iostream> using namespace std; int main() { int lhs = 0, rhs = 0; // get first value cout << “Enter an integer: “ cin >> lhs; // get second value cout << “Enter another integer: “ cin >> rhs; // compute arithmetic int add = lhs + rhs; int sub = lhs - add; // output results cout << lhs << “ + “ << rhs << “ = “ << add << endl << lhs << “ - “ << add << “ = “ << sub << endl; return 0; }
Which outputs (with inputs -4
and 10
):
example % ./a.out Enter an integer: -4 Enter another integer: 10 -4 + 10 = 6 -4 - 6 = -10
In addition to integers, C++ supports floating-point arithmetic, which allows for calculations involving decimal values. Floating-point variables—typically declared as float
or double
—can use the same standard arithmetic operators as integers:
Description | Operator | Example |
---|---|---|
addition | + | 1.1 + 2.2 = 3.3 |
subtraction | - | 1.1 - 2.2 = -1.1 |
multiplication | * | 1.1 * 2.2 = 2.42 |
division | / | 1.1 / 2.2 = 0.5 |
Unlike integer arithmetic, floating-point division preserves the decimal component (known as the mantissa) instead of discarding it. This makes floating-point numbers ideal for calculations that require precision—such as scientific measurements, financial figures, and averages.
It's important to note that there is no modulus operator (%
) for floating-point types in C++. This is because modulus is fundamentally a remainder operation after whole-number division. With floating-point division, there is no "remainder" in the same sense—division includes the entire result, including the fractional part.
Floating-point operations follow the same operator precedence rules as integers, but they may yield slightly imprecise results due to how decimal values are stored in binary format. This is a normal behavior in all programming languages and is rooted in how computers represent floating-point numbers using the IEEE 754 standard.
Here’s a simple example of floating-point division:
#include <iostream> using namespace std; int main() { double lhs = 0, rhs = 0; // get first value cout << “Enter a floating-point value: “ cin >> lhs; // get second value cout << “Enter another floating-point value: “ cin >> rhs; // compute arithmetic double div = lhs / rhs; // output results cout << lhs << “ / “ << rhs << “ = “ << div << endl; return 0; }
Which outputs (with inputs -4
and 10
):
example % ./a.out Enter a floating-point value: -4 Enter another floating-point value: 10 -4 / 10 = 0.4
This example shows that unlike integer division, the result -0.4 includes the fractional portion and does not truncate toward zero. This behavior is essential for accurate calculations in real-world applications where precision matters.
Arithmetic is not always performed between exclusively integers or exclusively floating-point values. Integers and floating-point values can be mixed in the same expression, for example:
floating-point OP integer
Or:
integer OP floating-point
When integers and floating-point values are mixed in the same expression it is called a mixed expression. Mixed expressions always result in a floating-point result. This is because the computer performs an implicit typecast, when a piece of data’s type is automatically changed to another type, on the integer changing it to a floating-point value, causing the expression to result in a floating-point answer:
Mixed Expression | Result |
---|---|
1.1 + 2 | 3.1 |
1 - 2.2 | -1.8 |
1.1 * 2 | 2.2 |
10.0 / 3 | 3.33... |
Since the computer implicitly typecasts the integer to become a floating-point value, mixed expressions are essentially floating-point expressions. Thus the modulo operator %
can not be used with mixed expressions.
The arithmetic operators can be combined in a single expression to create more complex equations. Take the equation for a line:
y = mx + b;
In order to write an expression in C++ that can perform this equation first variables for y
, m
, x
, and b
must be created:
#include <iostream> using namespace std; int main() { // variables double y = 0.0, m = 0.0, x = 0.0, b = 0.0; return 0; }
Values can then be read into the variables from the user:
#include <iostream> using namespace std; int main() { // variables double y = 0.0, m = 0.0, x = 0.0, b = 0.0; // read slope, x, and y-intercept cout << “Slope: “; cin >> m; cout << “X-Value: “; cin >> x; cout << “Y-Intercept: “; cin >> b; return 0; }
The values can then be used in the equation of a line, using the basic arithmetic operators to build a more complex equation:
#include <iostream> using namespace std; int main() { // variables double y = 0.0, m = 0.0, x = 0.0, b = 0.0; // read slope, x, and y-intercept cout << “Slope: “; cin >> m; cout << “X-Value: “; cin >> x; cout << “Y-Intercept: “; cin >> b; // calculate y-value y = m * x + b; // output results cout << m << “ * ” << x << “ + ” b << “ = “ << y << endl; return 0; }
Here, just like in math, the multiplication will be performed prior to the addition because the arithmetic operators follow PEMDAS precedence:
Thus, even if the operation were performed:
... // calculate y-value y = b + m * x; ...
The multiplication would still be performed prior to the addition. Like in math, to perform the addition first it would need to be surrounded with parenthesis:
... // calculate y-value y = (b + m) * x; ...
C++ provides compound assignment operators as a shorthand for performing an operation and assignment in a single step. Each compound operator corresponds to a basic arithmetic operator and follows this general form:
x = x OP y
can be rewritten more concisely as:
x OP= y
When using a compound assignment, any expression on the right-hand side of the operator is evaluated first. Then, the specified arithmetic operation (OP
) is performed using the current value of x
and the result of the right-hand expression. The final value is then stored back into x
, overwriting its previous value.
For example:
int x = 5, y = 10; x += y;
In this code, the value of y
is added to x
resulting in 15
, which would be stored in x
.
Here are the five primary compound assignment operators in C++:
Operation | Operator | Long Form | Compound Form |
---|---|---|---|
Addition | += | x = x + y | x += y |
Subtraction | -= | x = x - y | x -= y |
Multiplication | *= | x = x * y | x *= y |
Division | /= | x = x / y | x /= y |
Modulus | %= | x = x % y | x %= y |
These operators provide a cleaner, more concise way to update a variable’s value based on its current value.
Compound assignment is particularly helpful in longer expressions where you want to simplify your code while still respecting operator precedence. For example:
x = x * (y + z - 5);
Can be written more compactly as:
x *= y + z - 5;
Here, the expression on the right—y + z - 5
—is fully evaluated first. Then the result is multiplied by the current value of x
, and that product is stored back into x
.
Keep in mind: parentheses may still be required if you want to control the order in which operations happen within the expression. The compound operator itself does not change the precedence of operations.
Operator - A symbol that tells the compiler to perform a specific mathematical or logical operation on one or more operands.
Operand - A value on which an operator performs an operation.
Integer Arithmetic - Arithmetic involving whole numbers such as int
or long long
types.
Addition (+
) - An arithmetic operator that adds two values.
Subtraction (-
) - An arithmetic operator that subtracts the right operand from the left.
Multiplication (*
) - An arithmetic operator that multiplies two values.
Integer Division - Division where both operands are integers, resulting in an integer with the decimal part discarded.
Modulus (%
) - An arithmetic operator that returns the remainder of an integer division.
Floating-Point Arithmetic - Arithmetic involving numbers with decimal points, such as float
or double
types.
Floating-Point Division - Division where at least one operand is a floating-point value, resulting in a decimal value.
Mantissa - The decimal (fractional) part of a floating-point number.
Mixed Expression - An arithmetic expression that combines integer and floating-point values.
Implicit Typecast - The automatic conversion of one data type to another by the compiler, such as converting an integer to a floating-point value in a mixed expression.
Complex Expression - An arithmetic expression involving multiple operators combined into a single line or statement.
PEMDAS - A rule defining the order of operations in arithmetic: Parentheses, Exponents, Multiplication and Division, Addition and Subtraction.
Compound Assignment Operator - An operator that combines an arithmetic operation with assignment in a single step.
Shorthand Notation - A more concise way of writing code that achieves the same result as a longer form.
What is an operator, and how is it different from an operand?
Why are arithmetic operators considered binary operators?
How do arithmetic operators interact with variables in an expression?
Which arithmetic operator in C++ returns only the remainder of a division?
Which C++ operator has the lowest precedence: addition or multiplication?
What is the purpose of the modulus operator, and when should it be used?
Why does C++ use symbols like *
for multiplication instead of the letter x
?
What distinguishes arithmetic operators from assignment operators in terms of functionality?
Why does integer division in C++ discard the decimal part of the result?
When performing 17 / 5
, why is the result 3
and not 3.4
?
Why doesn’t integer division automatically round to the nearest whole number?
What are the limitations of using only integer arithmetic in calculations?
What values will a % b
never return, regardless of input? Why?
How is the result of a % b
affected if a
is negative?
Can the modulus operator ever return a value equal to the denominator? Why or why not?
Suppose a student claims that 9 / 2 = 4.5
in C++. What misunderstanding do they have?
How does floating-point division differ from integer division in terms of data types and results?
Why is the %
operator not available for floating-point types in C++?
What happens if a floating-point value is divided by an integer? What type is the result?
Explain why 1.0 / 2
results in a decimal value while 1 / 2
does not.
What is a mantissa, and why is it important when dealing with floating-point division?
Why are floating-point values more appropriate for scientific or financial calculations?
What is a mixed expression in C++, and what determines the type of the result?
In an expression with both int
and double
, why is the int
converted automatically?
How does implicit typecasting affect precision and accuracy in arithmetic?
Why are mixed expressions typically evaluated as floating-point expressions?
If double d = 5 / 2;
is used, what value is stored in d
and why?
Explain the role of operator precedence in evaluating the expression 3 + 4 * 2
.
How would the result of 3 + 4 * 2
change if parentheses were added?
Why does the expression m * x + b
correctly follow mathematical precedence rules?
If you want addition to happen before multiplication in a + b * c
, what must you do?
Can the placement of parentheses in an arithmetic expression change the final result? Explain.
What would happen if you omitted parentheses in an equation where they were needed?
Why is understanding PEMDAS important in C++ arithmetic?
Why might using integer division in a GPA calculator be a bad idea?
If a program uses int
for all math, what types of bugs might you encounter when working with decimals?
Why might a programmer prefer double
over int
in an equation like y = mx + b
?
Give an example of a real-world situation where using %
is more useful than /
.
How can small decisions about data types (like choosing int
vs double
) affect a program’s output?
Why is it not always safe to assume that division in code will behave like division in math class?
You are designing a program to calculate tax. Why is using integer arithmetic alone insufficient?
If a student says that 10 % 4.0
should be valid, how would you correct them?
A peer tells you that they can use int
types everywhere to save memory. What trade-offs should they consider?
You are debugging a program that calculates grades and find that it’s rounding down. What might be causing this?
Your code calculates 2 / 5
and stores the result in a double
. Why does the answer come out as 0.0
?
What is a compound assignment operator, and why might it be used instead of a longer form assignment?
What does the expression x *= y + 3
do, and in what order are the operations performed?
Why doesn’t a compound assignment operator change the normal rules of operator precedence?
What is the difference between x = x + y
and x += y
? Are they functionally the same?
Can you use compound assignment operators with expressions on the right-hand side, such as x += y * 2
? What happens first?
For each question, write the final value of the expression exactly as C++ would evaluate it. Assume all variables are integers unless otherwise stated.
3 + 5 =
10 - 2 =
4 * 3 =
12 / 4 =
13 % 5 =
4 + 3 + 2 =
10 - 3 - 2 =
2 * 3 * 4 =
12 / 2 / 2 =
3 + 4 * 2 =
8 - 2 * 3 =
6 + 9 / 3 =
10 - 6 / 2 =
8 % 3 + 2 =
9 - 7 % 4 =
4 + 3 * 2 - 1 =
12 / 3 + 6 % 4 =
5 + 6 / 3 * 2 =
10 - 2 + 4 * 2 =
9 % 4 * 3 + 2 =
(3 + 4) * 2 =
(8 - 2) * 3 =
(6 + 9) / 3 =
(10 - 6) / 2 =
(8 % 3) + 2 =
(9 - 7) % 4 =
4 + (3 * 2) - 1 =
(4 + 3) * (2 - 1) =
12 / (3 + 1) + 2 =
(10 - 2) * 2 + 1 =
(9 % (4 + 1)) + 1 =
((3 + 4) * 2) - 1 =
5 + ((2 + 3) * 4) =
((9 - 6) * (8 / 2)) + 3 =
12 / ((3 + 1) + 1) =
3 + 4 * 2 - (1 + 1) =
((10 - 2) * 2 + 4) / 3 =
((5 + 1) * (2 + 3)) % 7 =
20 / (2 * (2 + 3)) =
18 % ((2 + 1) * 2) =
For the following, what is the value of x?
int x = 4; x += 3;
int x = 10; x -= 7;
int x = 5; x *= 2 + 3;
int x = 20; x /= 4 + 1;
int x = 17; x %= 5 + 1;
int x = 3; x *= 2 + 4;
int x = 5; x += 3 * 2 - 1;
int x = 20; x /= 2 + 3 * 2;
int x = 10; x %= 4 + 2 * 2;
int x = 7; x += (2 + 3) * 2 - 1;
int x = 4, y = 3; x *= y + 2 * (y - 1);
int a = 6; a *= a - 2 + 1;
int x = 12; x /= (2 + 4) - 1;
int x = 11; x %= 3 + 2 * 2;
int x = 2, y = 3, z = 4; x += y * z - x;
```cpp
int result = 10 / 3;
cout << result << endl;
```
108. Why does the following code output 1
?
```cpp
int result = 10 % 3;
cout << result << endl;
```
109. What value is stored in x
?
```cpp
int x = 8 + 4 * 2;
```
110. What value is stored in x
?
```cpp
int x = (8 + 4) * 2;
```
111. What is the value of a
after this code runs?
```cpp
int a = 5;
a = a + 3 * 2;
```
112. What is the output of this program if the user enters 5
and 2
?
```cpp
double a, b;
cin >> a >> b;
cout << a / b << endl;
```
113. Why does the following print 0
instead of a decimal?
```cpp
int a = 5;
int b = 2;
double result = a / b;
cout << result << endl;
```
114. What needs to be changed in this code so that it outputs 2.5
?
```cpp
int a = 5;
int b = 2;
cout << a / b << endl;
```
115. What is the type and value of c
after this code?
```cpp
int a = 7;
double b = 2.0;
auto c = a / b;
```
116. What happens in this code, and why is the result not 2.5
?
```cpp
double result = 5 / 2;
```
117. Why does this code produce a floating-point result?
```cpp
double result = 5 / 2.0;
```
118. Why does this code compile but give an incorrect result?
```cpp
double result = 3 / 4;
cout << result << endl;
```
119. What is the bug in this program?
```cpp
int a = 10;
int b = 0;
int result = a / b;
```
120. What is the difference in output between these two snippets?
**Snippet A:**
```cpp
int a = 11;
int b = 3;
cout << a / b << endl;
```
**Snippet B:**
```cpp
double a = 11;
double b = 3;
cout << a / b << endl;
```
121. Why does this print 0.0
even though the result is stored in a double
?
```cpp
int a = 2;
int b = 5;
double result = a / b;
cout << result << endl;
```
122. What value is printed? Why?
```cpp
int a = 10, b = 2;
a *= b + 3;
cout << a;
```
123. What is the output, and in what order were the operations evaluated?
```cpp
int x = 5;
x += 2 * 3;
cout << x;
```
124. What is the final value of x
?
```cpp
int x = 15;
x /= 2 + 1;
```
125. What value is printed, and how were the compound assignments applied step by step?
```cpp
int x = 10;
x -= 2;
x *= 3;
cout << x;
```
Create a program that computes and prints the sum of 8 + 12
.
Create a program that subtracts 4
from 17
and stores the result in a variable called result
. Print result
.
Compute the product of 5
, 6
, and 2
, store the result in a variable, and output it.
Compute 10 divided by 3
using integer division and print the result.
Compute 13 modulo 5
and print the remainder.
Use variables a = 2
, b = 3
, and c = 4
. Compute and print the result of a + b * c
.
Use variables x = 10
, y = 2
, and z = 5
. Compute and print the result of x - y * z
.
Compute the result of the expression ((7 + 2) * 3) - 4
and output it.
Ask the user for two integers. Compute and print their sum.
Ask the user for two integers. Compute and print the result of dividing the first by the second (use integer division).
Ask the user for two numbers. Output their sum, difference, product, quotient, and remainder.
Ask the user to input three integers. Compute and print the result of a + b * c
.
Ask the user to input the width and height of a rectangle. Compute and print the area. The area of a triangle is where b
is the base value and h
is the height of the triangle.
Ask the user for two integers and print the result of (a + b) * (a - b)
.
Ask the user to enter three numbers: base, height, and offset. Compute and print base * height + offset
.
Ask the user for two integers and print both the quotient and remainder.
Ask the user how many pencils they have and how many students are in the class. Print how many each student would get if everyone got the same amount using integer division. Also print how many pencils would be left over using modulo.
Ask the user how many apples they have and how many friends they want to share them with. Print how many apples are left over after even sharing.
Create a program that asks for the user's age and prints how many months old they are (assume 12 months per year).
Ask the user how many minutes they’ve spent studying and print how many full hours and leftover minutes that is.
Ask for two integers and compute the average. Use integer division.
Ask the user to input the number of pages in a book and how many pages they read each day. Output how many full days it will take if you only use integer division.
Ask the user to input a 4-digit number. Print the sum of its individual digits using arithmetic (hint: use %
and /
to get the individual digits).
Create a program that sets x = 4
and y = 2
, then uses compound assignment to multiply x
by y + 1
. Print the final value of x
.
Ask the user for two integers, then use a compound assignment to add the second to the first and print the result.
Ask the user for three integers a
, b
, and c
. Update a
using the expression a *= b + c
and print the final value of a
.
Ask the user for a number of minutes. Use a compound assignment to convert the value to seconds and print the result.