Skillversitas

C++ Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on operands. C++ provides standard arithmetic operators such as addition, subtraction, multiplication, division, and modulus (remainder). In addition, C++ also has increment and decrement operators, which are used very frequently in loops and counter manipulation.

Arithmetic operators work on numeric data types such as int, float, double, and char. When the operators work on different data types, C++ performs implicit type conversion (automatic type conversion) following the type promotion rules.

Basic Arithmetic Operators

Operator Name Description
+ Addition Add two values
- Subtraction Subtract two values
* Multiplication Multiply two values
/ Division Divide two values
% Modulus Remainder of a division (integers only)

Code Example

#include <iostream>
using namespace std;

int main() {
    int a = 17, b = 5;

    // Basic operators
    cout << "=== Basic Arithmetic Operators ===" << endl;
    cout << "a = " << a << ", b = " << b << endl;
    cout << "a + b = " << (a + b) << endl;
    cout << "a - b = " << (a - b) << endl;
    cout << "a * b = " << (a * b) << endl;
    cout << "a / b = " << (a / b) << endl;
    cout << "a % b = " << (a % b) << endl;

    // Division with different types
    cout << "\n=== Type Promotion ===" << endl;
    double x = 17.0;
    int y = 5;
    cout << "17.0 / 5   = " << (x / y) << endl;
    cout << "17 / 5     = " << (a / b) << " (integer division)" << endl;

    // Increment and decrement operators
    cout << "\n=== Increment & Decrement ===" << endl;
    int c = 10;

    // Postfix increment
    cout << "c++ = " << c++ << " (the old value is returned first)" << endl;
    cout << "after c++: c = " << c << endl;

    // Prefix increment
    cout << "++c = " << ++c << " (the new value is returned first)" << endl;
    cout << "after ++c: c = " << c << endl;

    // Postfix decrement
    cout << "c-- = " << c-- << " (the old value is returned first)" << endl;
    cout << "after c--: c = " << c << endl;

    // Prefix decrement
    cout << "--c = " << --c << " (the new value is returned first)" << endl;
    cout << "after --c: c = " << c << endl;

    // Difference between prefix and postfix
    cout << "\n=== Prefix vs Postfix Difference ===" << endl;
    int p = 5, q = 5;
    int result1 = p++ + 10;
    int result2 = ++q + 10;
    cout << "p++ + 10 = " << result1 << " (p is now: " << p << ")" << endl;
    cout << "++q + 10 = " << result2 << " (q is now: " << q << ")" << endl;

    return 0;
}

Code Explanation

  • Addition (+): Adds two operands. Works on all numeric types.
  • Subtraction (-): Subtracts the second operand from the first operand.
  • Multiplication (*): Multiplies two operands.
  • Division (/): Divides two operands. For integers, the result is rounded down (truncation). For a decimal result, use the float or double type.
  • Modulus (%): Returns the remainder of a division. Can only be used on integer types. Very useful for determining whether a number is odd or even.
  • Type Promotion: When an operator works on different types (for example double and int), the lower type is automatically promoted to the higher type.
  • Postfix (c++): Returns the old value first, then increments by 1.
  • Prefix (++c): Increments by 1 first, then returns the new value.

Everyday Use Cases

Simple Calculator:

#include <iostream>
using namespace std;

int main() {
    double num1, num2;
    char operator1;

    cout << "=== Simple Calculator ===" << endl;
    cout << "Enter the first number: ";
    cin >> num1;

    cout << "Enter an operator (+, -, *, /): ";
    cin >> operator1;

    cout << "Enter the second number: ";
    cin >> num2;

    double result;

    if (operator1 == '+') {
        result = num1 + num2;
    } else if (operator1 == '-') {
        result = num1 - num2;
    } else if (operator1 == '*') {
        result = num1 * num2;
    } else if (operator1 == '/') {
        if (num2 != 0) {
            result = num1 / num2;
        } else {
            cout << "Error: Cannot divide by zero!" << endl;
            return 1;
        }
    } else {
        cout << "Invalid operator!" << endl;
        return 1;
    }

    cout << "Result: " << num1 << " " << operator1 << " " << num2 << " = " << result << endl;

    return 0;
}

Temperature Conversion:

#include <iostream>
using namespace std;

int main() {
    double celsius, fahrenheit, reamur, kelvin;

    cout << "Temperature Conversion" << endl;
    cout << "Enter the temperature in Celsius: ";
    cin >> celsius;

    fahrenheit = (celsius * 9.0 / 5.0) + 32;
    reamur = celsius * 4.0 / 5.0;
    kelvin = celsius + 273.15;

    cout << celsius << "°C = " << fahrenheit << "°F" << endl;
    cout << celsius << "°C = " << reamur << "°R" << endl;
    cout << celsius << "°C = " << kelvin << " K" << endl;

    return 0;
}

Conclusion

Arithmetic operators are the foundation of every mathematical operation in C++. The basic operators include +, -, *, /, and %. The increment (++) and decrement (--) operators have two variations: prefix and postfix, which differ in the order in which the value is returned. It is important to pay attention to type promotion when performing operations on different data types, and to always be careful about division by zero.