C++ supports the following arithmetic operators:
+ addition
– subtraction
* multiplication
/ division
% module
Let’s demonstrate the various arithmetic operators with the following piece of code.
Example:
#include <iostream.h>
int main ()
{
int a=5, b=3;
cout<<”a + b = “<<a+b;
cout<”\na – b =”<<a-b;
cout<<”\na * b =”<<a*b;
cout<<”\na / b =”<<a/b;
cout<<”\na % b =“<<a%b;
return 0;
}
Output:
a + b = 8
a – b =2
a * b =15
a / b =2
a % b=1
As shown above, all the other operations are straightforward and the same as actual arithmetic operations, except the modulo operator which is quite different. Modulo operator divides a and b and the result of the operation is the remainder of the division.