C Programming

Modulo Operator in C with Examples

It is a mathematical operator that uses the modulus operator. The remainder is determined by it. The % sign is used to represent it. The leftover could be zero in some situations, indicating that the integer is entirely divided by a divisor. The modulo operator is used in conjunction with arithmetic operators. Among two operands, the modulo operator operates. The modulo operator determines the residual of the integer by dividing the numerator utilizing a denominator. The leftover has always been an integer number. When there is no residual, it returns 0(zero) as that of the result.

Let’s get started by logging in from the Ubuntu 20.04 Linux system and opening the terminal shell via the activity bar or a shortcut key Ctrl+Alt+T. Make sure that your system must have GCC compiler configured on it before going further. If not, install it via the below query. After that, you are good to go with the examples of Modulo operator in C language.

$ sudo apt install gcc

Example 01

Let’s take a closer look at some examples of modulo operators in the C programming language. As the terminal has been opened, you should create a C-type file to write some C language code in it. In Linux, the “touch” command is widespread in the creation of such sort files. Hence, we have utilized it In our implementation and created a new file named “new.c”.

$ touch new.c

You can find your newly created file in the Linux home directory through File Manager. While using the terminal, we can also open the newly created file by just typing a simple command as below. This will open it in a GNU nano editor used to edit such sort of files.

$ nano new.c

Now the file has been opened in the GNU editor; you can easily type any code in it. Therefore, we have added a below simple code in it. This code contains a header file for standard input and output, and then the primary method has been defined. In the main way, we have simply put a printf statement to calculate the modulus of two random numbers using the percentage operator within them and print them. The primary method ended. You can save the file with “Ctrl+S” and close it with “Ctrl+X” to come back to the terminal shell.

After saving this file, compile it with the “gcc” compiler in the shell. The compiling of a file shows no error; this means the code is logically and syntactically correct. After that, execute your file with the “a.out” instruction. The output shows the modulus “8” of two numbers, “8” and “17”.

$ gcc new.c
$ ./a.out

Example 02

Our first example was a straightforward and to-the-point calculation of modulus by two random numbers. Let’s take another example to see the concept of modulus. In this example, we will be using the variables to find out the modulus. Hence, open the file “new.c” with the nano editor as per the following command:

$ nano new.c

The file is opened now. Update your file with the below-shown code. This code contains a header file and the main function. The main method includes three integer-type variables defined at the start of it. After that, we have assigned values to two of the variables, “a” and “b.” Then we have calculated the modulus of both the variables and assigned the estimated modulus value to a third variable which is “z” in our case. Then a printf statement has been used to print the modulus value saved in the variable “z.” Then we have again calculated the modulus of both the variables “a” and “b” by changing their positions this time. Again printed the calculated modulus saved in the variable “z.” After this, we have assigned new values to both the variables “a” and “b.” Then we have calculated a new modulus of both the newly assigned variables again and print them. At last, the primary method has been closed, and we have saved the file again with Ctrl+S. Jump back to the terminal using Ctrl+X.

Now compile the above code with the gcc compiler and then execute the file. The output is given below. We can see the three results generated by the three-time calculated modulus in our terminal.

$ gcc new.c
$ ./a.out

Example 03

This time, we will check whether the modulus results in the same on every data type or something new. So open your file once again as follows:

$ nano new.c

Now the file is opened in the GNU editor as below. Write out the below code in it. This time we have used the same standard header file and the main function in a C script. But the change is in the data type of variables that have been declared in the code. We have used the float data type to find out the modulus and assigned float values to variables “a” and “b.” Then we have used the third variable, “z” to save the value of a modulus resulted from both the variables. Printf statement is being used to print out the modulus in the terminal. The function ends here. Save the code and quit the file by Ctrl+S and Ctrl+X consequently.

Upon compiling the above C-type file, we have got an error saying that we have used the invalid operator on float type data. This means we cannot calculate the modulus of float type data. So to calculate modulus, we must provide the integer type data.

$ gcc new.c

Example 04

After checking the valid data type for calculating modulus, let’s look at negative integer type variables. In this example, we will calculate the modulus of negative integer data types. Hence, open the code file again.

$ nano new.c

Now the file is opened, update it with the below-shown C script, and save it via the “Ctrl+S” key. The overall code is the same, but we have defined one negative integer and one positive integer this time. We have also calculated the modulus two times in this example by changing the values of the variables “a” and “b.” Print statement has been used to show the modulus calculated by both variables and saved in the “z” variable. Exit the file via Ctrl+X.

Compilation and then executing a code have given us modulus output in a negative and positive value.

$ gcc new.c
$ ./a.out

Example 05

Let’s have an example of taking modulus from array type values. Pen the file to do so.

$ nano new.c

In the mentioned code, we have defined an integer type array with 6 integer values. Then we have used for loop to print and calculate the modulus of each value with the number 5.

The output gives us the 6 output modulus of 6 array integer numbers.

$ gcc new.c
$ ./a.out

Conclusion

At last, we have done with all the simple and most straightforward examples of calculating modulus in C language. I hope you find this article helpful and easy to use.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.