# C++: Mathematical Operators

Operators are simply symbols that perform a certain function - some of these perform mathematical functions. In this tutorial, we're going to build on your knowledge of user input, outputting data, and variables, to learn about some of the basic mathematical operators.

C++, like most programming languages, works on the concept of 'expressions' - combinations of symbols which represent values. If a simple mathematical statement is written, for example `5+5`, the calculation will be evaluated by the C++ compiler, and the result put in the place of the expression - in this case, 10. The `+` operator which is used to add two number values, be these constants (or literals) like '5', or variables of supported types (e.g. `int`, `double`, or `float`), can be shown to behave in this way with a simple `cout`. Take the following in which '14' is outputted instead of the actual text "8+6":

 ```1 ``` ``````cout << 8+6 << endl; ``````

The use of operators is often dependent on the data-type of whatever the operators are being used upon. Using the `+` operator on a string, for example, has an entirely different effect in which more string data can be "stuck on" to the string. Take for example the following:

 ```1 2 ``` ``````string one = "Hello"; cout << one + " Bob!" << endl; ``````

Note that in the above that the 'expression' type functionality is extremely important. The expression, `one + " Bob!"` is being evaluated to `"Hello Bob!"` and then outputted, however the variable "one" is not being changed. If we wanted to actually change the value of the variable "one" we would have to set it using something like the equals operator:

 ```1 2 3 4 ``` ``````string one = "Hello"; one = one + " Bob!"; cout << one << endl; ``````

There is also another mathematical operator related to `+` which can do the "one = one +" functionality for us - this is the `+=` operator. So instead of typing something like the above, we could instead use something a little bit cleaner like:

 ```1 2 3 4 ``` ``````string one = "Hello"; one += " Bob!"; cout << one << endl; ``````

The four basic mathematical operators, as you may expect, are those of addition, subtraction, multiplication, and division. These are represented by the `+`, `-`, `*` and `/` operators as appropriate. Each of these also has a `+=` equivalent for setting variables to themselves, add, subtracted, multiplied by, or divided by, some other value - these are `+=`, `-=`, `*=`, and `/=` as appropriate. There is also another mathematical operator which we're going to cover in this tutorial, which is called the modulus operator. This does division of whole numbers and gives the remainder of that division - for example `5 % 3` will be '2' as 3 goes into 5 once, with 2 remaining. This task may seem quite niche at first, however can become quite useful in a number of tasks.

All of these operators can be used with either variables or literals of the correct type (in this case, you'll want to use those of a 'number' type), and perhaps the easiest way to show the functionality of these operators is to simply give an example of some expressions using them, `cout`ing the results:

 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ``` ``````#include using namespace std; int main() { cout << "7 + 2 = " << 7 + 2 << endl; cout << "7 - 2 = " << 7 - 2 << endl; cout << "7 * 2 = " << 7 * 2 << endl; cout << "7 / 2 = " << 7 / 2 << endl; cout << "8 / 2 = " << 8 / 2 << endl; cout << "7 % 2 = " << 7 % 2 << endl; return 0; } ``````

So looking at the above, do they all output as expected? The majority do, however the `7 / 2` calculation outputs "incorrectly" as '3'. This is because our application knows it's dealing with whole number division (as we've specified integer literals), and so it rounds the result to a whole number. To solve this problem if this functionality is not what we want, as is the case here, we can simply change the literals to those of the `double` type by changing the calculation to `7.0 / 2.0`. This change means that the calculation outputs as we would expect.

So now that we know how to use basic mathematical operators in C++, let's put this to some use! Before we create a "big" application demonstrating a number of the operators we've covered - I just want to give a moment for the modulus operator, who I feel may be under-appreciated at this point. A great use for it is checking if a whole number is even or not -- if the number divides by 2 with no remainders, i.e. `number % 2 is equal to 0` the number must be even, otherwise it must be odd. We don't know how to check conditions like this quite yet, but come back to this after going through the next tutorial if you have some extra time, and try to create a basic program which takes input into an integer variable and then outputs if the number entered is even or not.

So back to creating an application with some practical use! I thought it might be a nice idea to create a program which uses the famous Pythagorean Theorem to calculate the longest side of a right-angled triangle given the other two sides. If you are unfamiliar with the equation, it states that the square of the longest side of a right-angled triangle is equal to the added squares of the other two sides. So if the longest side is 'c' and the other two are 'a' and 'b', the equation would be:

a2 + b2 = c2

So let's first get our application set up! Let's just think about includes for a second - we're definitely going to need `iostream` for outputting data and getting data from the user, but we're also going to need to somehow get the square root of a variable or expression to calculate the length of the longest side of the triangle. This functionality, along with some other advanced mathematical stuff, is stored inside the `cmath` header file, and so we should include this in our code too.

 ```1 2 3 4 5 6 7 8 9 10 11 12 ``` ``````#include #include using namespace std; int main() { //Main application code will go here return 0; } ``````

Note that with `cmath` included, we can square root things using the `sqrt` function. You can do what is called 'calling' a function by specifying the function name, followed by whatever you need to give the function to do it's job in brackets. If you don't need to give the function any values, you can just give empty brackets - `()`. In our case, we want to call the `sqrt` function with the value of the square of 'a' plus the square of 'b', and this expression will evaluate into the value we're looking for - the length of the longest side. Before this however, let's just use our knowledge of `cin` and `cout` to prompt the user for the first two sides (which, since they may be decimals, should probably be of type `double`):

 ```1 2 3 4 5 6 7 8 ``` ``````double a; double b; cout << "Side one: "; cin >> a; cout << "Side two: "; cin >> b; ``````

As talked about previously, we can now simply make use of the `sqrt` function to square root the added squares of these values. Squaring something is just multiplying it by itself, and thus we want the `sqrt` of `a*a + b*b` - hence `sqrt(a*a + b*b);`. Once again the 'expression' type functionality comes in handy as the simpler mathematics is first evaluated to give the result, and then this is passed to the `sqrt` function and square rooted to give us the result. Some people may prefer using another variable to deal with the result and get rid of some of the nested calculations in brackets - but I don't think such clutter is necessary here. The full code for our application can be seen below:

 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ``` ``````#include #include using namespace std; int main() { double a; double b; cout << "Side one: "; cin >> a; cout << "Side two: "; cin >> b; cout << "The hypotenuse (longest side) is of length: " << sqrt(a*a + b*b) << endl; return 0; } ``````