Arithmetic operators help you do math with numbers in your program. They let you add, subtract, multiply, divide, and find remainders easily.
0
0
Arithmetic operators in C++
Introduction
Calculating the total price of items in a shopping cart.
Finding the average score of a student.
Counting how many days are left until an event.
Splitting a bill evenly among friends.
Converting units, like inches to centimeters.
Syntax
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).
Examples
Adds 5 and 3 to get 8.
C++
int sum = 5 + 3; // sum is 8
Subtracts 4 from 10 to get 6.
C++
int diff = 10 - 4; // diff is 6
Multiplies 7 by 2 to get 14.
C++
int product = 7 * 2; // product is 14
Divides 20 by 4 to get 5, and finds remainder of 20 divided by 3, which is 2.
C++
int quotient = 20 / 4; // quotient is 5 int remainder = 20 % 3; // remainder is 2
Sample Program
This program shows how to use all the basic arithmetic operators with two numbers, 15 and 4.
C++
#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; }
OutputSuccess
Important Notes
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.
Summary
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.