The demand for the profession of a programmer is growing every year. At the moment, about a dozen languages of different levels are actively used to write codes. In order to make the process of teaching computer programming more efficient, senior and 1-2 year students are taught to create their first native applications in Pascal. This article is devoted to operations of div and mod and other calculations in its environment.
A few words about Pascal
Pascal was created in 1968-1969 by the famous scientist Nicklaus Wirth, who was subsequently awarded the Thuring Prize and the Pioneer of Computer Hardware medal. The latter, shortly before, participated in the development of the Algol-68 language standard. In an article published in 1970, Wirth called the main goal of his work the creation of an effective tool that uses structural programming and data.
Subsequently, the Pascal language had a huge impact on the field of information technology, becoming one of the basic. And to this day, in many leading universities in the world, training on professional programming is conducted on its basis.
What is integer division?
In mathematics, this name refers to the operation on two integers. As a result of the integer division of one of them into another, is the whole part of their quotient. In other words, if:
24: 6 = 4;
100: 3 = 33
55: 6 = 9;
and so forth
Integer division is also called finding an incomplete quotient.
Please note that with this operation, if the dividend is less than the divisor, the result is zero.
Denote the result of integer division of a by b, as q. Then
i.e., division is carried out in the usual sense with subsequent rounding of the result to the nearest integer in a smaller direction.
Pascal div operation
In the language we are considering, a special operator is provided for integer division - div. In Pascal, the expression, the formula of which is presented above, will look like:
q: = a div b.
If we are talking about constants, for example, a = 50, and b = 9, then we will have q: = 50 div 9. As a result, q will be equal to 5.
Balance calculation
The div operation in Pascal is usually learned with mod. Before figuring out what this record means, we’ll figure out how to find the remainder of the number.
Obviously, it can be found using the value obtained as a result of integer division, i.e.
r = a - bx q.
Operation mod in Pascal
Finding the remainder in Pascal is easy. For these purposes, the binary operation mod.
It is written as follows:
r = a mod b.
If, for example, a = 50, and b = 9, then we will have r: = 50 mod 9. As a result, r will be equal to 4.
Practical use
Finding the remainder of the division (r) is used in computer technology and in telecommunications. Using this operation, control and random numbers are generated in a limited range.
The mod operator is also used to determine the multiplicity of numbers, i.e., the divisibility of one number by another with an integer result. Obviously, these are pairs of numbers for which the result of applying the mod operator gives 0.
In Pascal, the multiplicity condition can be written as follows:
if a mod b = 0 then write (a, 'multiple', b).
For example, when you run the code with the condition written above, with values a = 4 and b = 2, the inscription “4 times 2” will be displayed on the monitor.
In addition, the mod operator can be used to output the last digit of a number in decimal notation. To do this, use the construction r = a mod 10. For example, the command r = 37 mod 10 will give the result 7.
Trunc statement
There is another operator with which you can get the same result as from the div in Pascal. It's about trunc, which applies not only to integers. It gives the result as the integer part of the fractional argument. Together with the “ordinary” division operator, the same result is obtained. Consider the above with an example. Let a = 51 and b = 9. Then, as a result of the q: = 51 div 9 command, we get q: = 5 resulting from rounding. If we apply the trunc operator to the same numbers, then q: = trunc (51/9) gives q: = 5, i.e., we have the same result.
Example 1
Consider how you can use div and mod in Pascal to solve practical problems. Suppose you need to find the sum of the digits of a two-digit number. The line of reasoning should be as follows:
- as shown above, the last of the digits in the number record can be obtained by applying the mod operator to it and to the number 10;
- as for the first number, it will turn out if you replace mod with the div command in Pascal.
We write the code in Pascal. It will look like this:
program Sum_2; (the name of the program)
var Number, Number1, Number2, Sum: integer; (enumeration of variables and determination of their type as integer)
begin (the beginning of the program body)
write ('Input Two-digit number'); (displaying the phrase "Input Two-digit number" on the screen)
read (Number); (input of initial number)
Number1: = Number div 10; (calculation of the first digit)
Number2: = Number mod 10; (calculation of the second digit)
sum: = Number1 + Number2; (calculation of the sum of digits)
write (Sum); (output to the screen)
end.
For the number 25, the result of using this program will be 7, and, for example, for 37 - 9.
Example 2
We will write a code for a program that calculates the sum of the digits of a 3-digit number.
How to find the last digit is understandable. The 1st calculation is not difficult either. It will turn out as a result of applying the div operator in Pascal to this number and to 100. It remains to figure out how to find the second digit. To do this, you can use a more complex construction, which will turn out if you apply the div operator to the original number and 10, and then the mod operator to the result and 10.
The program code for calculating the sum of the digits of a three-digit number will look like this:
program Sum_3; (the name of the program)
var Number3, Sum: integer; (enumeration of variables and determination of their type as integer)
begin (the beginning of the program body)
write ('Input Tree-digit number'); (displaying the phrase “Input Tree-digit number” on the screen)
read (Number3); (input of initial number)
Sum: = Number3 div 100 + Number3 mod 10 + Number3 div 10 mod 10; (calculation of the amount)
write ('Sum); (output to the screen)
end.
Some notes
Note that the normal division operation when applied to integer arguments goes beyond their class. This fundamentally distinguishes it from the div operation in Pascal, as well as from the mod operator, which produce a result that is also an integer.
The order of operations of binary type (i.e., performed on 2 operands) in a complex expression is determined by their priority and parentheses. In other words, in the presence of brackets, the expressions in them are first calculated in order from left to right. In this case, the operations *, /, mod and div are more priority than + and -. If there are no brackets, then first, from left to right, you should perform actions with high priority, and then - + and -.
Now you know what the div function in Pascal is used for. You are also aware of the possibilities that the mod operator provides, which will surely help you when creating your own applications.