ComputersProgramming

What is a div in Pascal? Additions, calculations and examples

Every year, the demand for a programmer's profession is growing. At the moment, about ten languages of different levels are actively used for writing codes. In order to make the learning process of computer programming more effective, senior students and students of the 1-2 year are taught to create their own first applications in the language of Pascal. The given article is devoted to operations div and mod and other calculations in its environment.

A few words about Pascal

"Pascal" was created in 1968-1969 by the famous scientist Niklaus Wirth, who was subsequently awarded the Thuring Prize and the "Pioneer of Computer Technology" medal. The latter, shortly before that, participated in the development of the language standard "Algol-68". In an article published in 1970, the main goal of his work Wirth called the creation of an effective tool that uses structured programming and data.

Subsequently, the language "Pascal" had a huge impact on the information technology, becoming one of the basic. And to this day in many leading universities of the world it is on its basis that professional programming is being taught.

What is an integer division

In mathematics, this name is understood as an operation on two integers. As a result of the integer division of one of them into another, is the whole part of their particular. In other words, if:

24: 6 = 4;

100: 3 = 33

55: 6 = 9;

And others.

An integer division is also called finding an incomplete quotient.

Note that with this operation, if the dividend is less than the divisor, the result is zero.

We denote the result of the integer division of a by b, as q. Then

That is, division is carried out in the usual sense, with a subsequent rounding of the result to the nearest whole to a smaller side.

The div operation in Pascal

In the language we are considering, a special operator is provided for the 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.

Calculating the remainder

The div operation in Pascal is usually studied along with mod. Before we find 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, that is,

R = a - bx q.

The mod operation in Pascal

In Pascal you can find the remainder very simply. For these purposes, a binary operation mod is provided.

It is written as follows:

R = a mod b.

If, for example, a = 50, and b = 9, then we have r: = 50 mod 9. As a result, r will be 4.

Practical use

Finding the remainder of the division (r) is used in computer technology and telecommunications. With this operation, control and random numbers are generated in a limited range.

The mod operator is also used to determine the multiplicity of numbers, that is, 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, if you run the code with the condition written above, for a = 4 and b = 2, "4 times 2" will be displayed on the monitor.

In addition, the mod operator can be used to output the last digit of a decimal number. To do this, use the r = a mod 10 construction. For example, the command r = 37 mod 10 will produce the result 7.

The trunc operator

There is another operator, with which you can get the same result as the div in "Pascal". It's about trunc, which applies not only to whole numbers. It outputs the result as an integer part of a fractional argument. Together with the operator of "ordinary" division, one and the same result is obtained. Let's consider the told on an example. Suppose that a = 51, and b = 9. Then, as a result of the command q: = 51 div 9, we get q: = 5, resulting from rounding. If we apply trunc to the same numbers, then q: = trunc (51/9) gives q: = 5, that is, we have the same result.

Example 1

Consider how you can use div and mod in Pascal to solve practical problems. Let it be necessary to find the sum of the digits of a two-digit number. The course of reasoning should be as follows:

  • As already shown above, the last of the digits in the number entry can be obtained by applying to it and to the number 10, the mod operator;
  • As for the first number, it will be obtained by replacing mod with the div command in Pascal.

We will write down the code in the language Pascal. It will look like this:

Program Sum_2; (the name of the program)

Var Number, Number1, Number2, Sum: integer; (Enumeration of variables and definition of their type, as an integer)

Begin (the beginning of the body of the program)

Write ('Input Two-digit number'); (Output to the screen of the phrase "Input Two-digit number")

Read (Number); (Input of the original number)

Number1: = Number div 10; (Calculation of the first digit)

Number2: = Number mod 10; (Calculation of the second digit)

Sum: = Number1 + Number2; (Calculating the sum of digits)

Write (Sum); (Displaying the result on the screen)

End.

For the number 25 the result of using this program will be 7, and, for example, for 37 - 9.

Example 2

Let's write the code for the program that calculates the sum of the digits of the 3-digit number.

How to find the last digit is understandable. It is not difficult to calculate the first. It will result from 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 is obtained by applying the div operator to the original number and by 10, and then to the result and to the 10 operator mod.

The code for calculating the sum of 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 definition of their type, as an integer)

Begin (the beginning of the body of the program)

Write ('Input Tree-digit number'); (Output to the screen of the phrase "Input Tree-digit number")

Read (Number3); (Input of the original number)

Sum: = Number3 div 100 + Number3 mod 10 + Number3 div 10 mod 10; (Calculation of the sum)

Write ('Sum); (Displaying the result on the screen)

End.

Some remarks

Note that the normal division operation when applied to integer arguments goes beyond their class. This radically differs from the div operation in Pascal, as well as from the mod operator, which produces a result that is also an integer.

The order of execution of binary type operations (i.e., executing over 2 operands) in a complex expression is determined by their priority and parentheses. In other words, in the presence of parentheses, the expressions in them are first evaluated in order from left to right. In this case, the operations *, /, mod and div are more priority than + and -. If there are no brackets, first, from the left to the right, you should perform actions with a higher priority, and then - with + and -.

Now you know what the div function in Pascal is used for. You also know the possibilities that the mod operator gives, which, for sure, will help you when creating your own applications.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.birmiss.com. Theme powered by WordPress.