·  C++ – Syntax – Operators – Arithmetic

 

- Info:

  - This tutorial shows how to use Arithmetic Operators in C++.: - + - * / % ++ -- !.

  - If one of the operands is float or double the other one is implicitly transformed into that type.

 

- Example:

  - Create C++ Console Application Test.cpp.

 

    Test.cpp

    #include <stdio.h>

    #include <iostream>

    using namespace std;

   

    int main() {

   

      //TEST VARIABLES.

      int     x          = 10;

      int     y          = 20;

   

      //ARITHMETIC OPERATORS.

      int     negate     =  -y;        // -20   = -20

      int     add        = x+y;        //  30   = 10+20

      int     subtract   = x-y;        // -10   = 10-20

      int     multiply   = x*y;        // 200   = 10*20

      int     divide1    = x/y;        //   0   = 0.5     = 10/20

      float   divide2    = (float)x/y; //   0.5 = 10.0/20.0

      int     reminder   = x%y;        //  10   = 0*20+10 = 10%20

      int     increment1 = ++x;        //  11 Increment x by 1 and then store x into increment1.

      int     decrement1 = --x;        //  10 Decrement x by 1 and then store x into decrement1.

      int     increment2 =   x++;      //  10 Store x into increment2 and then increment x by 1.

      int     decrement2 =   x--;      //  11 Store x into decrement2 and then decrement x by 1.

   

      //DISPLAY RESULTS.

      printf("%i \n", negate);

      printf("%f \n", divide2);

      cout << subtract << endl;

   

      return 1;

   

    }