The basics of programming in c++ for beginners

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 ++

23 thoughts on “Arithmetic operations in C ++

  1. First number 10
    The second number 4
    I do not understand the first
    The first operation of addition = 14 converges
    The second substraction = 10 (and should be 6)
    The third operation = 40 converges
    The fourth step = 10 (and should be 2)
    Fifth opreratsii = 2 (converges)

    1. there is no 6 must, and 10 as 14 – 4 = 10,
      10 * 4 = 40
      40 / 4 = 10
      10 % 4 = 2 ( 4 * 2 = 8 with 10 8 and thrown 2 remains )

  2. Variable number1 accumulates. Because these values ​​are obtained and. After each operation, it was necessary to reset the variable – “number1=0”. Then I would consider as the beginning. Author of the post forgot to mention it.

    1. Then, in this case, it makes no sense to shorten (+=, *= et al) so, as a result, you always need to add a line with zeroing. Or just add number3 = 0 to the beginning, and also write n3 = n1 + n2. Something is not as clear as possible about this topic with these abbreviations…

  3. Link to the next lesson goes to the list of tasks by topic, most likely you need to make a transition to tasks on the current topic.

    1. as I understand it, when executing += new number1=14 and when executing -= 14-4=10, a new number1 =10 is obtained

  4. Mining 19 450 Dollars. GET >> https://forms.yandex.com/cloud/65e622855056901a794151f0?hs=af47879f97c20fea39f9ef70ff7f76cd& says:

    0c3vgd

Leave a Reply to Alexey Cancel reply

Your email address will not be published. Required fields are marked *