Back to Course

Introduction to C++

0% Complete
0/0 Steps
  1. C++: Basics
    5Topics
    |
    6 Quizzes
  2. C++: Compilation and Execution
    5Topics
    |
    6 Quizzes
  3. C++: Variables
    9Topics
    |
    10 Quizzes
  4. C++: Conditions Operators
    5Topics
    |
    6 Quizzes
  5. C++: Logical Operators
    3Topics
    |
    4 Quizzes
  6. C++: Loops and Errors
    8Topics
    |
    9 Quizzes
  7. C++: Vectors
    4Topics
    |
    3 Quizzes
Lesson 3, Topic 5
In Progress

C++: Arithmetic Operators

Yasin Cakal 5 Nov 2021
Lesson Progress
0% Complete

We know that computers are great calculators. Let’s learn to use arithmetic operators to make calculations in C++.

Let’s first go through different arithmetic operators and their uses in C++:

  • + is used for adding values
  • – is used for subtracting values
  • * is used to calculate the product of two values
  • / is used to divide values
  • % is used to calculate the modulo (i.e., it divides the values and loads the remainder in the variable)

Let’s look at a simple example. Let’s add 5 and 9. For this, we type 5+9; in the int main () function.

To understand assignment operator and arithmetic operator, let’s try the following code:

#include <iostream>
using namespace std;
 
int main() 
{
    int marks=0;
    marks=5+2;
    //the assigned value is now 7
    marks=marks-5;
    //the new assigned value is now 2 because 7-5=2
    marks=marks*3;
    //the new assigned value for marks is now 6 because 2*3=6
    std::cout<<marks;
    return 0;
}

Output:

6

Now, you can understand how the assignment operator and arithmetic operators work.

Things to Remember: The order in which the operations carry out can be controlled using brackets like writing 5*(2-5) instead of 5*2-5.

Responses

Leave a Reply

%d bloggers like this: