Arithmetic operations in C ++




arithmetic in c ++In modern life, it is very difficult to do without arithmetic operations. We constantly have something to consider: fold, multiply, subtract, share, etc.. Programming – not an exception. you in 99.9% cases have to use them, when writing your programs. They should not be afraid of – all arithmetic operations – easy, understandable and familiar to us from school.

Consider the arithmetic operation in the following table.

arithmetic operations with
Arithmetic operations in C ++ – dr. 1

Here, particular attention should be paid to the modulus (%). This operation is often used in solving various tasks. An example of its application: if we need to divide modulo 9 on 4 (9 % 4), the result is 1 (this residue – then, What's on 4 It is no longer divided into whole). More examples: 20 % 8 = 4 ( 8 placed in the 20 2 fold: 8 * 2 = 16, 20 – 16 = 4 remainder of the division ), 3 % 2 = 1, 99 % 10 = 9, 9 % 10 = 9. Important:

  • modulo division applies only to integer variables ;
  • can not be divided according to the module 0;

Example:

The result of compilation:
arithmetic operations with 2

Here you can see, the fission num1 on num2, it appeared on the screen only the integer part of – 4 (although the exact value 4.5). The fractional part is cut, because the variables are defined, as integer – int. As a result of the modulo we see 2 – what is left in the remainder of the division 18 on 4.

Another thing I would like to consider in this article – so-called combined (or compound) statements. In addition to its role of arithmetic, they simultaneously act as a variable assignment. Here is a list of composite statements:

arithmetic operations with
Arithmetic operations in C ++ – dr. 2

Illustrate:

Although for someone, at first sight, these statements may seem confusing, trust, to them very quickly and you can get used to successfully apply in their programs. You just need some practice with their use. Your code will be more compact look. Same, the use of combined statemants is a sign of good programming. Therefore,, Though not an error in the code separately from the use of the addition assignment – number1 = number1 + number2;, preferable to use a shortened version of the recording number1 = number2;

Result:

arithmetic operations with

In this article only examined binary statements – those which are used for arithmetic operations with two variables (operand). In one of the following, we look at unary statemants (for operations on one variable) – increment and decrement, and next – and ternarnım statement (which requires three operands).

It is desirable to consolidate the knowledge acquired practice – Tasks: Arithmetic operations in C ++

Subscribe to notifications about new articles on our website:


Agree to receive notifications from purecodecpp.com to my e-mail

10 thoughts on “Arithmetic operations in C ++

  1. super, enough available all painted! It is only necessary to pay attention that a simple binary statement type ” + ” It is doing simple addition, and an integral type binary statament ” += ” apart from the addition of (or straight.) More and assigns the value of the first variable (in this case number1) each time when calculating…
    thanks to the author!

  2. And I liked it, I understand everything perfectly, there are binary statements, They are used for two variables, say in your example: number1 = number2; there are two variables, binary means, and there is the unary ternary, Here is an example of a unary: 34++ or 23–, and maybe so: ++34 or –23; Example ternarnogo: ? first statement : the second statement. General thank you for everything, I will continue to study

    1. Operations carried out all right. Share your results, if you do not like this.

      At the = sign can be used any binary operations … for example:
      =&& , =| , =% , =<> …
      And still it gives correct results … Only they must be correctly interpreted (that should have).

    1. Ну такговно” – оно и в Африке говно … : проходите мимо, не задерживайтесь!

  3. Всё очень доступно и понятно, большое спасибо за уроки.

Leave a Reply

Place code in tags: <pre class="lang:c++ decode:true ">YOUR CODE</pre>