Last Updated:

Div and mod operations in Pascal

In our article, we will analyze operations such as div and mod in Pascal. Now you will learn what it is and why these operations are needed.

Div and mod operations in Pascal

Div, mod or normal division

In general, div and mod are very similar to the usual division that we are taught in school. But if they exist for some reason, then there are still differences. And already at the very beginning I really want to note that both operations work only with integers (integer).

Div statement in Pascal

It's actually pretty simple. Div is used to find the whole part of the division.

Here's an example of the code to understand:

program enteryourname1;
uses crt;
var
n : integer;
begin
n := 9;
n := n div 4;
end.

As a result, our n will be equal to two. If you divide 9 by 4, you get 2.25. That is, by applying the div operation in Pascal, you will get the result up to a comma (an integer part of a number).

Mod operator in Pascal

 

This operation works a little the other way around – it is already looking for the remainder of the division. It happens that beginners in Pascal mistakenly think that the mod is looking for a fractional part. No, it's not.

Let's take the same code as an example:

program enteryourname2;
uses crt;
var
n : integer;
begin
n := 9;
n := n mod 4;
end.

As a result, we get n, which is equal to one. How did that come about? When we divide 9 by 4, we can take two times 4 (you get 8). But we are left with a unit that is not divisible by 4. This is the "unit" that mod will look for, or, to put it another way, the remainder of the division of your numbers.

In general, mod in Pascal is often used to determine the multiplicity of numbers. Note that a number is a multiple if its remainder is zero.

 

Using the div and mod operators when solving problems

 

We will give some examples of using this program in order to make it even easier for you to understand how it works.

program enteryourname3;
uses crt;
var A,B,C: integer;
begin
clrscr;
A := 13;
B := 5;
C := A div B;
writeln (‘13 div 5 = ‘, C);
C := A mod B;
writeln (‘13 mod 5 = ‘, C);
readln;
end.

Thus, using div, we get answer 2 (13 divided by 5 equals 2.6; we only need the whole part). And using mod, we get a result of 3 (the remainder of division).

Here's another example:

program enteryourname4;
uses crt;
var A,B,C: integer;
begin
clrscr;
A := 290;
B := 11;
C := A div B;
writeln (‘290 div 11 = ‘, C);
C := A mod B;
writeln (‘290 mod 11 = ‘, C);
readln;
end.

Of course, the program will calculate everything itself. But once again we clarify: here div will be equal to 26 (we take only the whole part), and mod - 4 (the remainder of division).

We hope that we have answered your question about what mod and div mean in the Pascal program, and thus were able to facilitate your work in this program. You can leave your suggestions in the comments.