Scripting

Modulus in C Language

In this Linux Ways article, you will learn how to get the remainder after a division between two variables. We will show you the easiest way to get a module using the “%” operator and also the mathematical formula to get it by applying it, so you can see the different possibilities you have to solve the remainder of a division.

We’ll then put what we learned into practice with several practical examples using the code and images, so you’ll understand how each method works and be able to use them quickly.

Module or Remainder After Division

The rule for exact division of integers is that the divisor must be a multiple of the dividend. When we work with integers and perform the division operations, in cases where the dividend is not divisible by the divisor, the result is an integer approximation. From this division, we are left with a remainder that is less than the divisor.

This remainder, also called modulus, affects the accuracy of the operations because, as we already said, the result of a division that has a remainder is only an integer approximation.

Formula to Determine the Modulus of a Division

One way to get the modulus of a division is to subtract the result of the division from the dividend multiplied by the divisor. The following is the formula to obtain the modulus of “a” divided by “b” using this method:

mod = a -(a/b)*b;

Now, let’s see how to apply this formula in a simple example that gives the modulus of the division of 27 divided by 5:

#include <stdio.h>

void main ()
{
int a =27;
int b =5;
int d;
int mod;
d = a/b;

mod = a -(a/b)*b; //Get module

printf ("a / b = %i\n", d);  
printf ("mod = %i\n", mod);  
}

The following is the execution of this code where we can see the result of “a” divided by “b” or the remainder of the division of 27 over 5:

The “%” Operator to Obtain the Module of a Division

The C language provides the “%” operator to obtain the modulus in a division. Because of its simple syntax, the use of this operator to obtain the modulus of a division is the most commonly used in the C language. The following is the expression that is used to obtain the modulus of “a” divided by “b”:

mod = a % b;

NOTE: The “%” operator that is used under this expression refers to a mathematical operator that has nothing to do with the format identifiers that are used in the strings of the printf() function to specify the output format.

How to Get the Modulus of a Division with the “%” Operator

In this example, we will show you how to use the “%” operator to get the modulus of a division. To do this, we specify the integer that we want the dividend to be and give it a random odd value.

We also declare the “b” integer which will be the divisor that we will assign with a random even value. We store the result in the “mod” integer which we then output to the screen using the printf() function.

The next step is to apply the operation with the “%” operator to the “a” and “b” variables and display the result in the command console. Let’s see the following code for this example:

#include <stdio.h>

void main (){
int a =27;
int b =4;
int mod;

mod = a -(a/b)*b;
 
printf ("mod = %i\n", mod);  
}

The following image illustrates the execution of this code:

As we can see in the figure, the division of an odd number by an even number is not exact, which is why it resulted in a remainder or modulus which, in this case, is equal to 3.

How to Know If a Number Is a Multiple of Another with the “%” Operator

The “%” operator allows us to solve various programming tasks such as determining whether one value is a multiple of another. To do this, we simply perform a modulo operation where a result that is equal to 0 means that the divisor is a multiple of the dividend.

In the following, we will see an example that lists the values from 0 to 255, which are multiples of 8, using a modulo operation and an “if” condition to determine them:

#include <stdio.h>

void main (){

int mod;

for (int a= 0; a != 255; a++){
     mod = a % 8;
     if (mod == 0)
        printf ("%i Is multiple of 8\n", a);  
    }
}

The following image shows the list of multiples of 8 which is generated with the “%” operator and the “if” condition:

Conclusion

In this Linux Ways article, we showed you how to get the module of a division with two different modes. We explained how to get a modulus by a simple operation with the “%” operator. To see the different ways to solve the same problem in this language, we also showed you the applied equation which is used for this purpose.

Also, as an example, we showed you how the “%” operator can be used to solve various problems such as finding the multiples of a given value using the simple conditionals and module operations.

Similar Posts