C++ Arithmetic Operators

Introduction

Arithmetic operators in C++ are used to perform basic mathematical operations on numeric values. These operators allow you to perform addition, subtraction, multiplication, division, modulus operations, and more. Understanding these operators is essential for manipulating data and performing calculations in your programs.

List of Arithmetic Operators

  1. Addition (+): Adds two operands.
  2. Subtraction (-): Subtracts the second operand from the first.
  3. Multiplication (*): Multiplies two operands.
  4. Division (/): Divides the first operand by the second.
  5. Modulus (%): Returns the remainder of the division of the first operand by the second.
  6. Increment (++): Increases the value of an operand by 1.
  7. Decrement (--): Decreases the value of an operand by 1.

Arithmetic Operators with Examples

Addition (+)

The addition operator adds two operands.

Example

#include <iostream>
using namespace std;

int main() {
    int a = 10;
    int b = 3;
    int sum = a + b; // Adds a and b

    cout << "Sum (a + b): " << sum << endl;
    return 0;
}

Output

Sum (a + b): 13

Subtraction (-)

The subtraction operator subtracts the second operand from the first.

Example

#include <iostream>
using namespace std;

int main() {
    int a = 10;
    int b = 3;
    int difference = a - b; // Subtracts b from a

    cout << "Difference (a - b): " << difference << endl;
    return 0;
}

Output

Difference (a - b): 7

Multiplication (*)

The multiplication operator multiplies two operands.

Example

#include <iostream>
using namespace std;

int main() {
    int a = 10;
    int b = 3;
    int product = a * b; // Multiplies a and b

    cout << "Product (a * b): " << product << endl;
    return 0;
}

Output

Product (a * b): 30

Division (/)

The division operator divides the first operand by the second. Note that if both operands are integers, the result will be an integer (fractional part will be discarded).

Example

#include <iostream>
using namespace std;

int main() {
    int a = 10;
    int b = 3;
    int quotient = a / b; // Divides a by b

    cout << "Quotient (a / b): " << quotient << endl;
    return 0;
}

Output

Quotient (a / b): 3

Modulus (%)

The modulus operator returns the remainder of the division of the first operand by the second.

Example

#include <iostream>
using namespace std;

int main() {
    int a = 10;
    int b = 3;
    int remainder = a % b; // Remainder of a divided by b

    cout << "Remainder (a % b): " << remainder << endl;
    return 0;
}

Output

Remainder (a % b): 1

Increment (++)

The increment operator increases the value of an operand by 1. It can be used as a prefix (++a) or postfix (a++).

Example

#include <iostream>
using namespace std;

int main() {
    int a = 10;
    a++; // Increments a by 1

    cout << "Increment (a++): " << a << endl;
    return 0;
}

Output

Increment (a++): 11

Decrement (--)

The decrement operator decreases the value of an operand by 1. It can be used as a prefix (--a) or postfix (a--).

Example

#include <iostream>
using namespace std;

int main() {
    int a = 10;
    a--; // Decrements a by 1

    cout << "Decrement (a--): " << a << endl;
    return 0;
}

Output

Decrement (a--): 9

Conclusion

Arithmetic operators are essential tools in C++ for performing basic mathematical operations. This chapter covered the addition, subtraction, multiplication, division, modulus, increment, and decrement operators with simple examples and their outputs. Understanding these operators will help you perform calculations and manipulate data in your programs effectively. In the next chapter, we will explore relational operators in C++.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top