# Mathematical Operations in C++

Mathematics is the fundamental (fundamental) unit of any programming. Development of bright and attractive interfaces, creation of online maps, writing games of any genre, development of operating systems, creation of sites and so on. In general, programming and mathematics are related to each other as the right and left hemispheres of the brain.

Certain operations can be performed on any data. Over numbers and numbers, you can perform various mathematical operations and transformations: add, subtract, divide, multiply. Here seem to be only four basic and well-known operations, but how many of them can be organized combinations ... Countless! **Operations nok** and **nod**, finding the factorial, calculation and many other variations.

The C++ language is a very powerful (if not the most powerful) programming language and has a number of its own individual features, including when working with numbers.

Consider a few examples: addition and subtraction of integers and fractional numbers, and division and multiplication with two types.

#include <iostream>

int main() {

setlocale(LC_ALL, "English");

int a = 10 + 20; //integer variable a put the result of adding two integers

int aa = 10.0 + 20.1; // put the result of adding two fractional numbers into an integer variable

int b = 30 - 15; //integer variable a put the result of subtracting two integers

int bb = 30.2 - 15.3; //integer variable a put the result of subtracting two fractional numbers

std::cout << "10 + 20 = " << a << "\n" << "10.0 + 20.1 = " << aa << "\n" << "30 - 15 = " << b < < "\n" << "30.2 - 15.3 = " << bb << "\n\n\n";

float c = 10 + 20; //we put the result of the addition of two integers into a float variable

float cc = 10.0 + 20.1; //we put the result of adding two fractional numbers into a float variable

float d = 30 - 15; //we put the result of subtracting two integers into a float variable

float dd = 30.2 - 15.3; //we put the result of subtracting two fractional numbers into a variable of type float

std::cout << "10 + 20 = " << c << "\n" << "10.0 + 20.1 = " << cc << "\n" << "30 - 15 = " << d < < "\n" << "30.2 - 15.3 = " << dd << "\n\n\n";

int e = 10 * 2; //integer variable e write the result of the multiplication of two integers

int ee = 10.1 * 2.4; // in the integer variable ee we write the result of the multiplication of two fractional numbers

int f = 10 / 3; //integer variable f write the result of dividing two integers

intff = 10.0 / 3.0; //in the integer variable ff we write the result of dividing two fractional numbers

std::cout << "10 * 20 = " << e << "\n" << "10.1 * 2.4 = " << ee << "\n" << "10 / 3 = " << f < < "\n" << "10.3 / 3.3 = " << ff << "\n\n\n";

float g = 10 * 2; //in a variable of type float we write the result of multiplying two integers

float gg = 10.1 * 2.4; //in a variable of type float we write the result of multiplying two fractional numbers

float h = 10 / 3; //in a variable of type float we write the result of dividing two integers

float hh = 10.0 / 3.0; //in a variable of type float we write the result of dividing two fractional numbers

std::cout << "10.0 * 20.0 = " << g << "\n" << "10.1 * 2.4 = " << gg << "\n" << "10.0 / 3.0 = " << h < < "\n" << "10.3 / 3.3 = " << hh << "\n\n\n";

system("pause");

return 0;

}

The result of the calculations will be as follows:

As you can see, if we write the result to an integer variable (types **int**, **long int**...), then the fractional part is discarded, if there was one during the calculation. On the one hand, it looks very stupid, but this is only at first, in the future you will like it very much and you will look for this feature everywhere.

### Priority of operations

In programming, as in mathematics, there is such a thing as "priority of operations".

What is the "priority of operations" and why did we mortals need it?

This concept came to us from mathematics. Mathematics is a science that loves order and organization. Some actions should be performed a little earlier, some a little later.

Parentheses have the highest priority— the actions in parentheses are performed first, then the multiplication and division actions are performed, and after the addition and subtraction actions.

Let's systematize this good:

The number on the left indicates the priority |

As an example of the application of mathematical operations and the final understanding of the priority of operations, let's make the computer calculate the result of the equation.

#include <iostream> |

### A short form of recording mathematical operations

In the C++ programming language, you can keep a brief record of mathematical expressions, this form significantly increases the speed of writing code.

For example, we need to increase a certain counter, that is, add a new one to the old value and write this result to the variable where the old value lay.

#include <iostream>

int main() {

setlocale(LC_ALL, "English"); // connect the Russian language

int count0 = 0; //first counter

int count1 = 0; //second counter

std::cout << "Old count0 = " << count0 << "\n"; //display the initial value of the first counter

std::cout << "Old count1 = " << count1 << "\n"; //display the initial value of the second counter

count0 = count0 + 1; //usual notation for addition

count1 += 1; //short form of addition

std::cout << "\nNew count0 = " << count0 << "\n"; //display the changed value of the first counter

std::cout << "New count1 = " << count1 << "\n\n"; //output the changed value of the second counter

system("pause"); //so that the program ends only after clicking

return 0;

}

Briefly you can write addition, subtraction, multiplication, division.

float a = 4.7; // variable with value

float c; // variable in which we write the result

c += a; //addition

c -= a; //subtraction

with *= a; //multiplication

with /= a; //division

thus we got acquainted with the most important operations of the C++ language and programming in general