Lesson 15 of 60 – Arithmetic Operators in C++
25%

Arithmetic Operators in C++

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.

Note: The main arithmetic operators in C++ are +, -, *, /, and %. The result of an operation depends on the types of its operands.

1. What are Arithmetic Operators?

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.

2. Types of Arithmetic Operators

Operator Name Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus a % b

3. Addition Operator +

The + operator adds two values.

int a = 10;
int b = 20;

int sum = a + b;

std::cout << sum;

The result is 30.

4. Addition of Variables

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.

5. Addition with Decimal Values

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.

6. Subtraction Operator -

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.

7. Subtraction Can Produce a Negative Value

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.

8. Multiplication Operator *

The * operator multiplies two values.

int price = 100;
int quantity = 5;

int total = price * quantity;

std::cout << total;

The result is 500.

9. Multiplication with Decimal Values

double price = 99.50;
double quantity = 2;

double total = price * quantity;

std::cout << total;

The result is 199.

10. Division Operator /

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.

11. Integer Division

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.

12. Floating-Point Division

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.

13. Division Using static_cast

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.

14. Modulus Operator %

The % operator returns the remainder after integer division.

int result = 17 % 5;

std::cout << result;

The result is 2.

15. Modulus with Even and Odd Numbers

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.

16. Modulus with Positive Numbers

Expression Result
10 % 3 1
15 % 4 3
20 % 5 0
25 % 6 1

17. Unary Plus Operator

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.

18. Unary Minus Operator

The unary - operator changes the sign of its operand.

int number = 10;

int result = -number;

std::cout << result;

The result is -10.

19. Arithmetic Expressions

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.

20. Using Parentheses

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.

21. Arithmetic Operators with Variables

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.

22. Arithmetic Operators with User Input

#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.

23. Simple Calculator

#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;
}

24. Division by Zero

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.

25. Arithmetic Operators and Data Types

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.

26. Compound Arithmetic Assignment

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.

27. Arithmetic Example with Marks

#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.

28. Complete Arithmetic Example

#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;
}

29. Common Arithmetic Operator Mistakes

  • Forgetting that integer division discards the fractional part.
  • Using the modulus operator with floating-point operands.
  • Dividing by zero.
  • Ignoring the effect of operator precedence.
  • Forgetting parentheses when a specific evaluation order is required.
  • Using a data type that cannot represent the required result correctly.

30. Arithmetic Operators – Final Summary

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

📌 Key Points

  • Arithmetic operators are used for mathematical calculations.
  • The + operator performs addition.
  • The - operator performs subtraction.
  • The * operator performs multiplication.
  • The / operator performs division.
  • The % operator gives the remainder for integer operands.
  • Integer division produces an integer result.
  • Use a floating-point operand when a decimal division result is required.
  • static_cast can be used to explicitly convert an operand before division.
  • Parentheses can be used to control the evaluation of arithmetic expressions.
  • Never divide by zero.
  • Choose appropriate data types for arithmetic calculations.

🧠 Quick Quiz

Question: Which arithmetic operator is used to find the remainder of an integer division?