C++ - arithmetic operators example
The example below shows the usage of arithmetic operators - addition(+), subtraction(-), multiply(*), division(/) and modulo(%) operators.
#include <iostream> using namespace std; int main (){ float a = 25; float b = 10; cout<<"a = "<<a<<", b = "<<b<<"\n\n"; //Add a and b float result_add = a + b; cout<<"a + b = "<<result_add<<"\n"; //Subtract b from a float result_sub = a - b; cout<<"a - b = "<<result_sub<<"\n"; //Multiply a and b float result_mul = a * b; cout<<"a * b = "<<result_mul<<"\n"; //Divide a by b float result_div = a / b; cout<<"a / b = "<<result_div<<"\n"; //return division remainder - works with integral operands, //for other types cmath's fmod function can be used int result_modulo = (int) a % (int) b; cout<<"a % b = "<<result_modulo<<"\n"; return 0; }
The output of the above code will be:
a = 25, b = 10 a + b = 35 a - b = 15 a * b = 250 a / b = 2.5 a % b = 5
❮ C++ - Operators