C Programming

Modulo Operator in C

The modulo operator is a commonly used arithmetic operator in programming languages. This operator is used to find out the remainder after we perform division between the two integer numbers or variables. For example, if we divide 5 by 2, we will get a remainder of 1, where 5 is the dividend, 2 is the divisor, and 1 is the remainder. In C programming language the modulo operator is represented by the percent symbol: %. One example use case of the modulo operator is to find whether the number is even or odd. We may also use this operator to find if the two numbers are factors of each other or not. The expression for the modulus operator is a% b = c here, and c is the remainder.

General Syntax

For a generalized arithmetic where its desired to do modulus we have the following math:

// create 2 numbers
NUMBER1
NUMBER2

// Divide one number by the other
NUMBER1 / NUMBER2

// Two outputs are created
RESULTS_OF_DIVISION
REMAINDER

The generalized syntax to do a modulo in C is as follows:

int NUMBER1;
int NUMBER2;

REMAINDER = NUMBER1 % NUMBER2;

This guide will let us know how we can use the modulus operator to develop various logic in our codes by demonstration of a few examples.

Example # 01

In the main, declare two variables “a” and “b”, then initialize them by assigning some numeric values, e.g., 5 and 4 respectively. After this initialization, introduce another variable of data type integer and name it modulo. Assign the modulus of a % b to this variable and display the modulus using the printf () function.

#include <stdio.h>

void main () {

    int a= 5;
    int b= 4;

    int modulus = a % b;

    printf ("Remainder of division %d, %d is %d\n", a, b, modulus);
}
linuxhint@:~$ ./modulo
Remainder of division 5, 4 is 1
linuxhint@:~$

The above example has returned the output 1, which is the remainder resulting from the modulus of 5 and 4.

Example # 02

If we are required to take the modulus of more than two variables at a time, then we can transform our modulus operator into the chain modulus operator. The chain Modulus operator takes the modulo between more than two variables simultaneously without wasting extra space and declaration time on each of the variables separately.

We will implement this type of modulus on the next example. We will include the important header files for the print and scan of the input and outputand then declare the four different variables a, b, c, and d with the data type as integer. After this declaration of variables, we will initialize these variables by assigning their values. Then we will take the collective modulus of all these variables in a chain and save the resulting value in another variable with data type int and then display this value as an output.

#include <stdio.h>

int main () {

    int a; int b; int c; int d;

    a= 5; b = 4; c=3; d=2;

    int modulo =a % b % c % d;

    printf ("%d\n", modulo);

    return 0;
}
linuxhint@u20:~$ ./modulo
1
linuxhint@u20:~$

The output displays the chain modulus of all the four variables a, b, c and d for the output which is a result of 1:

5%4 ==> 1
1%3 ==> 1
1%2 ==> 1

Example # 03

In this example, we will learn how we can use the modulus operator to verify if the number is even or odd. For this purpose, we will simply take the modulus of the number and if the result is 0 its an even number and if the result is 1 than its an odd number. We put this into a function and use the function to test various inputs. We also create a print display function for making the program more elegant:

#include <stdio.h>

void display(int input, int odd){
    if (odd == 1)
        printf("The input number %d is odd\n", input);
    else
        printf("The input number %d is even\n", input);
}

int is_odd(int input){
    int remainder = input % 2;
    // return of 1 when odd
    // return of 0 when even
    return remainder;
}

void main(){
    int result;
    int userinput;

    // test 1
    result = is_odd(1);
    display(1, result);

    // test 2
    result = is_odd(2);
    display(2, result);

    // test 3
    result = is_odd(3);
    display(3, result);

    // test 4
    result = is_odd(4);
    display(4, result);

    // test based on user input
    printf("Enter your number: ");
    scanf("%d", &userinput);
    result = is_odd(userinput);
    display(userinput, result);
}
sysads@u20:~$ ./mod
The input number 1 is odd
The input number 2 is even
The input number 3 is odd
The input number 4 is even
Enter your number: 33
The input number 33 is odd

Conclusion

The modulus operator is an arithmetic operator for the C language. This article highlights the use of the modulus operator for various functions. We have learned about the basic syntax and the representation of the modulus operator in C. You can also see a sample function with a modulus operation to check even or odd numbers and to take the simple and chain modulus for multiple variables.

About the author

Linux Wolfman

Linux Wolfman is interested in Operating Systems, File Systems, Databases and Analytics and always watching for new technologies and trends. Reach me by tweeting to @linuxhint and ask for the Wolfman.