Last Updated:

Mathematical Operations in C++

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.


The result of the calculations will be as follows:

As you can see, if we write the result to an integer variable (types intlong 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:

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.



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.


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