Module: (C++) Arithmetic expressions


Problem

7/7

Division on C++

Theory Click to read/hide

Peculiarities of the division operation in the C and the C++


In the C ++ programming language, there are two division operations:
/ division, and % calculation of the remainder of the division.
 
Remember!
1) The operation of calculating the remainder of division (%) is performed ONLY on integers!
2) The result of the division operation (/) depends on the type of operands.

The rule here is as follows
When dividing an integer by an integer, the fractional part is always discarded, regardless of what type of variable we store the value!
When saving the calculating result into an integer variable, the fractional part will also be discarded.


Let's analyze examples of performing division operations:
#include<iostream>
using namespace std;
int main()
{
  int i, n;
  double x;
  i = 7;
  x = i / 4; // x=1, divides the integer into the integer
  x = i / 4.; // x=1.75, divides the integer into the real 
              // (4 - without a dot is perceived as an integer, 
              // 4. (with a dot) - it's a real number!)
  x =(double) i / 4; // x=1.75, divides the real into the integer  
                    // - here the variable i is converted to a real number 
                    // - this is an explicit conversion of type
  n = 7. / 4.;      // n=1, the result is written to an integer variable
  return 0;
}

Problem

1) In lines 9, 11, 13 and 15, organize the output of the variable value calculated in the previous line (organize the output from a new line).
2) Run the program.
3) Make sure that the program works exactly as it is written in the theoretical part.
4) Analyze the answers.