Site icon TestingDocs.com

C++ Arithmetic Operators

Overview

In this tutorial, we will learn about C++ Arithmetic Operators with a sample program. Arithmetic operators are used to performing arithmetic calculations.

C++ Arithmetic Operators

 

Operation Operator Description Sample Example
Addition + Adds the two operands sum = a + b
Subtraction Subtracts the second operand from the first operand diff = a – b
Multiplication * Multiplies the two operands product = a * b
Division / Divides the numerator by the denominator div = a/b
Modulus % The remainder of the integer division a%b
Increment ++ Increments the value of the operand by 1 i++
Decrement Decrements the value of the operand by 1 j–

 

C++ Program

/************************************
* C++ Arithmetic Operators
* Filename: arithmetic.cpp
* C++ Arithmetic Operators Demo
* program.
* C++ Tutorials - www.TestingDocs.com
**************************************/
#include
using namespace std;
int main()
{
    //Declare two variables
    int a=20,b=5;
    //Used in Arithmetic calculations
    double product,div,rem;
    int sum,difference;
    //Perform Arithmetic calculations
    sum = a + b; // addition
    difference = a - b; // subtraction
    product = a*b; // product
    div = a/b; // divide
    rem = a%b;  // get remainder
    // Print Statements
    cout << "Sum = " << sum << endl;
    cout << "Difference = " << difference << endl;
    cout << "Product = " << product << endl;
    cout << "Division = " << div << endl;
    cout << "Modulus = " << rem << endl;
    cout << "Increment = " << ++a << endl; //pre-increment
    cout << "Decrement = " << --b << endl; //pre-decrement
    return 0;
} // end main


That’s it. Analyze the program output and understand how the operators work.

C++ Tutorials

C++ Tutorials on this website:

https://www.testingdocs.com/c-coding-tutorials/

For more information on the current ISO C++ standard

https://isocpp.org/std/the-standard

Exit mobile version