Arithmetic operators help you do math with numbers in your program. They let you add, subtract, multiply, divide, and find remainders easily.
Arithmetic operators in C++
result = a + b; // addition result = a - b; // subtraction result = a * b; // multiplication result = a / b; // division result = a % b; // remainder (modulus) - only for integers
Use % only with whole numbers (integers) to get the remainder.
Division with integers gives an integer result (no decimals).
int sum = 5 + 3; // sum is 8
int diff = 10 - 4; // diff is 6
int product = 7 * 2; // product is 14
int quotient = 20 / 4; // quotient is 5 int remainder = 20 % 3; // remainder is 2
This program shows how to use all the basic arithmetic operators with two numbers, 15 and 4.
#include <iostream> using namespace std; int main() { int a = 15; int b = 4; cout << "a + b = " << a + b << "\n"; cout << "a - b = " << a - b << "\n"; cout << "a * b = " << a * b << "\n"; cout << "a / b = " << a / b << "\n"; cout << "a % b = " << a % b << "\n"; return 0; }
Remember that dividing two integers gives an integer result, cutting off any decimal part.
Use floating-point numbers (like float or double) if you want decimals.
The modulus operator % only works with integers, not decimals.
Arithmetic operators let you do basic math in your code.
Use +, -, *, /, and % for addition, subtraction, multiplication, division, and remainder.
Division with integers drops decimals; use floating-point types for decimal results.
Practice
Solution
Step 1: Understand the meaning of each operator
The operator%returns the remainder after division, while/returns the quotient.Step 2: Identify the operator for remainder
Since remainder is asked, the correct operator is%.Final Answer:
% -> Option CQuick Check:
Remainder operator = % [OK]
- Confusing / with %
- Using * for remainder
- Using - for remainder
a and b in C++?Solution
Step 1: Recall multiplication operator syntax
In C++, multiplication uses the asterisk symbol*between operands.Step 2: Check each option
Onlya * b;uses the correct operator and syntax.Final Answer:
a * b; -> Option AQuick Check:
Multiplication operator = * [OK]
- Using x instead of *
- Writing multiply as word
- Using dot operator for math
int a = 10, b = 3; std::cout << a / b << std::endl;
Solution
Step 1: Understand integer division in C++
Bothaandbare integers, so division truncates decimals.Step 2: Calculate integer division result
10 divided by 3 is 3.333..., but integer division returns 3.Final Answer:
3 -> Option BQuick Check:
Integer division truncates decimals = 3 [OK]
- Expecting decimal result
- Using floating-point division without casting
- Thinking output is error
int x = 5; int y = 2; int z = x / y.0; std::cout << z << std::endl;
Solution
Step 1: Analyze the divisor
The expressiony.0y.0is invalid syntax in C++ because variables cannot have decimal points.Step 2: Identify the syntax error
The decimal point afterycauses a syntax error; correct would bey * 1.0orstatic_cast<double>(y).Final Answer:
Syntax error due to decimal point in divisor -> Option AQuick Check:
Variable names cannot have decimal points [OK]
- Thinking y.0 is valid float
- Ignoring syntax error
- Confusing variable and literal
int a = 7, b = 4;, which expression correctly calculates the average as a floating-point number in C++?Solution
Step 1: Understand integer vs floating-point division
Using2divides integers, truncating decimals. Using2.0forces floating-point division.Step 2: Evaluate each option
(a + b) / 2 does integer division, result 5. (a + b) / 2.0 divides by 2.0, giving 5.5 as float. Options C and D do not calculate average correctly.Final Answer:
(a + b) / 2.0 -> Option DQuick Check:
Use floating-point divisor for decimal average [OK]
- Using integer 2 causing truncation
- Wrong operator precedence
- Misinterpreting average formula
Ready to prove it?
Pass the test and this pattern moves unknown
