Arithmetic Operators in C++


TL ; DR:

  • We can tell computers to perform calculations for us using +, -, *, /:

    cout << 2 + 6; // Prints 8
    cout << 10 - 7; // Prints 3
    cout << 3 * 3; // Prints 9
    cout << 6 / 2; // Prints 3
    


  • We can also use variables. In these examples, C++ will replace variable num with its value 6 to do the calculations:

    int num = 6;
    cout << num + 5; // Prints 11
    cout << 10 - num; // Prints 4
    cout << 3 * num; // Prints 18
    cout << num / 2; // Prints 3
    





Full lesson:

Computers absolutely excel at performing calculations. The "compute" in their name comes from their historical association with providing answers to mathematical questions.

These calculations can be performed either directly with numbers or with variables that are assigned numeric values.

Arithmetic operators are used with numeric values to perform common mathematical operations:

Operator Name Examples
+ Addition 3 + 5,  x + 5,  3 + y,  x + y
- Subtraction 8 - 3,  8 - y,  x - 3,  x - y
* Multiplication 3 * 5,  3 * y,  x * 5,  x * y
/ Division 8 / 4,  8 / y,  x / 8,  x / y
% Modulus 8 % 4,   8 % y,  x % 4,  x % y


Note that performing arithmetic on variables does not change the variable - you can only update a variable using the = sign.

Let's go through some examples together to better understand how each operator works:


Addition

// Declare 2 integer variables and initialize them:
int a = 2;
int b = 3;

cout << a + b; // Output: 5

cout << a + 10; // Output: 12

Subtraction

// Declare 2 integer variables and initialize them:
int a = 2;
int b = 3;

cout << a - b; // Output: -1

cout << 10 - b; // Output: 7

Multiplication

// Declare 2 integer variables and initialize them:
int a = 2;
int b = 3;

cout << a * 5; // Output: 10

cout << 2 * a * b; // Output: 12

Division

In C++, division between integers returns only the integral part of the result:

// Declare 2 integer variables and initialize them:
int a = 2;
int b = 3;

cout << 20 / 3; // Output: 6

cout << b / a; // Output: 1

If we want to get the real result of the division, at least one of the terms has to be of type "double":

// Declare 2 integer variables and initialize them:
int a = 2;
int b = 3;

cout << (double) 7 / 3; // Output: 2.5

cout << b / (double) a; // Output: 1.5

Assignment
Follow the Coding Tutorial and let's practice with arithmetic operators!


Hint
Look at the examples above if you get stuck.


Introduction

In this lesson, we will explore arithmetic operators in C++. Arithmetic operators are fundamental in programming as they allow us to perform basic mathematical operations. These operators are essential for a wide range of applications, from simple calculations to complex algorithms. Understanding how to use arithmetic operators effectively is crucial for any programmer.

Understanding the Basics

Arithmetic operators in C++ include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). These operators are used to perform mathematical operations on numeric values. Here are some simple examples:

cout << 2 + 6; // Prints 8
cout << 10 - 7; // Prints 3
cout << 3 * 3; // Prints 9
cout << 6 / 2; // Prints 3

It's important to understand these basics before moving on to more complex aspects of arithmetic operations in C++.

Main Concepts

Let's define and explain the key concepts and techniques involved in arithmetic operations:

These operators can be applied to both constants and variables. For example:

int num = 6;
cout << num + 5; // Prints 11
cout << 10 - num; // Prints 4
cout << 3 * num; // Prints 18
cout << num / 2; // Prints 3

Examples and Use Cases

Let's go through some examples to demonstrate the use of arithmetic operators in various contexts:

Addition

int a = 2;
int b = 3;

cout << a + b; // Output: 5
cout << a + 10; // Output: 12

Subtraction

int a = 2;
int b = 3;

cout << a - b; // Output: -1
cout << 10 - b; // Output: 7

Multiplication

int a = 2;
int b = 3;

cout << a * 5; // Output: 10
cout << 2 * a * b; // Output: 12

Division

In C++, division between integers returns only the integral part of the result:

int a = 2;
int b = 3;

cout << 20 / 3; // Output: 6
cout << b / a; // Output: 1

If we want to get the real result of the division, at least one of the terms has to be of type "double":

int a = 2;
int b = 3;

cout << (double) 7 / 3; // Output: 2.5
cout << b / (double) a; // Output: 1.5

Common Pitfalls and Best Practices

When working with arithmetic operators, there are some common mistakes to avoid:

Best practices for writing clear and efficient code include:

Advanced Techniques

Advanced techniques related to arithmetic operations include:

For example, using compound assignment operators:

int a = 5;
a += 3; // Equivalent to a = a + 3; Output: 8
a *= 2; // Equivalent to a = a * 2; Output: 16

Code Implementation

Here are some well-commented code snippets demonstrating the correct use of arithmetic operators:

// Addition
int a = 2;
int b = 3;
cout << a + b; // Output: 5

// Subtraction
cout << a - b; // Output: -1

// Multiplication
cout << a * 5; // Output: 10

// Division
cout << 20 / 3; // Output: 6

// Modulus
cout << 20 % 3; // Output: 2

Debugging and Testing

When debugging code involving arithmetic operations, consider the following tips:

Writing tests for functions that use arithmetic operations is crucial. Here are some example test cases:

#include <cassert>

void test_addition() {
    int a = 2;
    int b = 3;
    assert(a + b == 5);
}

void test_subtraction() {
    int a = 2;
    int b = 3;
    assert(a - b == -1);
}

void test_multiplication() {
    int a = 2;
    int b = 3;
    assert(a * 5 == 10);
}

void test_division() {
    int a = 2;
    int b = 3;
    assert(20 / 3 == 6);
}

void test_modulus() {
    assert(20 % 3 == 2);
}

int main() {
    test_addition();
    test_subtraction();
    test_multiplication();
    test_division();
    test_modulus();
    cout << "All tests passed!" << endl;
    return 0;
}

Thinking and Problem-Solving Tips

When approaching problems related to arithmetic operations, consider the following strategies:

Conclusion

In this lesson, we covered the basics of arithmetic operators in C++. We explored addition, subtraction, multiplication, division, and modulus operations. Understanding these concepts is crucial for performing calculations in programming. We also discussed common pitfalls, best practices, advanced techniques, and provided code examples. By mastering these concepts, you will be well-equipped to handle a wide range of programming tasks.

Additional Resources

For further reading and practice problems, consider the following resources: