## Introduction to Prolog mod

Prolog mod is an Arithmetic tool used to find the modulus of two numbers. Prolog has a number of basic arithmetic functions for integer manipulations. Most of the prolog implementations provide tools to handle integers or floating-point numbers. In Prolog arithmetic tools, any variable must always be bounded and the value of these should be always numerical. Basically, the value of arithmetic tools bounds the first argument, if not, it will generate an error message as result. In Prolog arithmetic, symbols are a specific types of infix operators.

**Syntax:**

`X mod Y.`

X and Y are integers.

Or

`mod(X,Y).`

As per the syntax, we have two types, X mod Y or mod(X,Y).

In prolog, there are other arithmetic operations too such as addition, subtraction, multiplication, division, quotient, power, etc. Prolog mod, also known as modulo is used to obtain the remainder of the integer. The precedence of operators is the same as per other arithmetic operations. If integers are float, then we can use round/1 to round up to the next possible integer and float/1 can be used to convert integer to floating value.

### Examples of Prolog mod

Given below are the examples mentioned:

#### Example #1

Simple manner.

**Code:**

`?- X is 10 mod 6.`

**Output:**

So here as you can see X=4 is the modulus value of 10 and 6.

#### Example #2

Goal stored in .pl file.

**Step 1:** Create a file and name it accordingly with .pl extension.

**Step 2:** Add the below rule required for modulus operation.

**calcMod:**W is 70 mod 3,write(’70 mod 3 is ‘),write(W),nl.

This means that W is the resultant of modulus 70 and 3. Here “nl” is to go to the next line, and write(W) will print out the result onto the gnu compiler.

**Step 3:** On the GNU compiler, first we need to go to the path where the file is located, with the below command.

**Code:**

`?- change_directory('F:/XYZ/abc').`

XYZ and abc being the same names of folders.

**Step 4:** Then, access the required file, with the below command.

**Code:**

`?- [modulo].`

Here, modulo is the file name.

**Step 5:** As the modulo has a calcMod function to execute, that function is to be called in the gnu compiler to execute the modulus result. Follow the below command to execute the calcMod function.

**Code:**

`?- calcMod.`

**Step 6:** Function is executed and resultant is printed on the compiler.

**Output:**

So here, we have taken the command to execute mod for 70 and 3 into a file with .pl extension stored under prolog folder which is further stored under eduCBA folder. Calling the calcMod function has executed mod and the resultant value has been displayed as 1.

#### Example #3

For negative integers.

**Code:**

`?- X is -4 mod 3.`

`?- X is 4 mod -3.`

`?- X is -4 mod -3.`

**Output:**

So here, for each negative integer, the resultant is different as above. If both integers are negative, the resultant value is still negative.

41 Online Courses | 13 Hands-on Projects| 322+ Hours| Verifiable Certificate of Completion

4.5

View Course

#### Example #4

Arithmetic expression error.

**Code:**

`?- X is 5 mod 0.`

**Output:**

It gives below error as “uncaught exception: error(evaluation_error(zero_divisor),(is)/2)”.

#### Example #5

Another way of implementation.

**Code:**

`?- X is mod(5,3).`

**Output:**

So here we are using the mod(X, Y) way of implementation. 5 and 3 are passed as arguments to a mod function and X value is printed on the console.

Basically, the modulo operation will return the remainder or a signed remainder of the division, when one number is divided by the other, called as modulus of operation. This operator is distinguished by the symbol mod which represents the modulus for which it is operating.

### Conclusion

We have seen what Prolog mod is and how is it defined in Prolog. It is one of the arithmetic operators in Prolog to check the remainder value for divisibility of 2 numbers, also known as modulus of two numbers. The remainder can be a signed integer or an unsigned integer depending upon the two integer values. We have also seen the syntax of the Prolog mod in two ways and have implemented both the ways, represented in the above examples. We have also implemented using a .pl extension file which has the required modulo rule in it. We have also explained the steps to implement the modulo with .pl file.

### Recommended Articles

This is a guide to Prolog mod. Here we discuss the introduction and appropriate examples of Prolog mod for better understanding. You may also have a look at the following articles to learn more –