We can do arithmetic operations using arithmetic operators.
Some of them are:
+ // addition - // subtraction * // multiplication / // division % // modulo
Example:
#include <iostream> int main() /*w ww . ja v a2s. c o m*/ { int x = 123; int y = 456; int z = x + y; // addition z = x - y; // subtraction z = x * y; // multiplication z = x / y; // division std::cout << "The value of z is: " << z << '\n'; }
The integer division results in a value of 0.
It is because the result of the integer division where both operands are integers is truncated towards zeros.
In the expression x / y, x and y are operands and / is the operator.
To get a floating-point result, use the type double and make sure at least one of the division operands is also of type double:
#include <iostream> int main() /*from w ww . ja v a2 s .c o m*/ { int x = 123; double y = 456; double z = x / y; std::cout << "The value of z is: " << z << '\n'; }
Similarly, we can have:
#include <iostream> int main() /*w ww .j a va 2 s . co m*/ { double z = 123 / 456.0; std::cout << "The value of z is: " << z << '\n'; }
and the result would be the same.