Integer division and remainder


In the module "Arithmetic expressions" we talked about the features of the division operation in Pascal.
Recall that for integer data (type integer) you can use three division operations:
- normal division, returns a value of type real
div - integer division, when we discard the fractional part as a result of the division operation
mod - calculation of the remainder of division

REMEMBER!
In Pascal the result of dividing an integer by an integer – it's always a real number.

Example: var a, b, d, e: integer;   c:real; a := 10; b := 3; c := a / b; // Answer: s = 3.33333333333333E+000 d := a mod b; // Answer: d = 1 e := a div b; // Answer: e = 3 These operations are very important in programming. They need to be understood and used correctly. And that takes practice!

 

The need to apply the operation of calculating the remainder of the division is visible when working with the digits of a number. 

Let's analyze the following task:
A three-digit number is given. Display all the digits of this number and get a new number formed by permuting units and hundreds

The most difficult question that arises for beginners is how to take and get its digits from a number.
In fact, everything is solved quite simply, if you remember the math. And mathematics tells us that any number can be decomposed into a sum of digit terms.
For example: 365=3*100+6*10+5*1 . We see that each digit is a multiplier of the corresponding digit of the number. 
We will show how to get each digit of a number into a separate variable, using the example of dividing the columns by the number 10. (We take the number 10, because we have a decimal number system and, accordingly, we have digit terms 1, 10, 100, etc. )
  

Analyzing the figure, you can see that 
e := n mod 10; // operation n mod 10 - calculates the last digit of the number n (that is, the units of the number) 365 mod 10 = 5 

d := n div 10 mod 10; // operation n div 10 - reduces the number by 10 times, i.e. discards the last digit of the number (365 div 10 = 36), 
                   // now we can calculate the number of tens by applying the familiar operation to the result - calculate the remainder after division by the number 10, 36 mod 10 = 6
 
s := n div 100; // to get hundreds, it is enough to discard two digits from the right of the number, that is, divide by 10 twice (n div 10 div10 or the same as n div 100) 365 div 100 = 3

Having the saved digits of the number, we can make any number out of them by multiplying the desired digit by the corresponding digit: 
for example, the line below will get a new number from the original number n, with hundreds and ones rearranged:
1) the old number of ones (stored in the variable e)  multiplied by 100 
2) the old number of tens (stored in the variable d)  multiply by 10 
3) we can simply multiply the old number of hundreds by 1, or simply take the value stored in the variable s
Then just add the values ​​from points 1, 2 and 3 and get a new number:

n1 := e * 100 + d * 10 + s;

The whole program will look like this:
var n, e, d, s: integer;

begin
  read(n);
  e := n mod 10;
  d := n div 10 mod 10;
  s := n div 100;
  writeln(e, ' ', d, ' ', s, ' ', e * 100 + d * 10 + s);
end.