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.

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”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/C-Modulo-Operator-Examples-01.png" data-lazy- height="31" src="data:image/svg xml,” width=”499″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/C-Modulo-Operator-Examples-02.png" data-lazy- height="29" src="data:image/svg xml,” width=”495″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/C-Modulo-Operator-Examples-03.png" data-lazy- height="233" src="data:image/svg xml,” width=”772″>

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”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/C-Modulo-Operator-Examples-04.png" data-lazy- height="87" src="data:image/svg xml,” width=”499″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/C-Modulo-Operator-Examples-05.png" data-lazy- height="29" src="data:image/svg xml,” width=”510″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/C-Modulo-Operator-Examples-06.png" data-lazy- height="539" src="data:image/svg xml,” width=”754″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/C-Modulo-Operator-Examples-07.png" data-lazy- height="145" src="data:image/svg xml,” width=”499″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/C-Modulo-Operator-Examples-08.png" data-lazy- height="29" src="data:image/svg xml,” width=”512″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/C-Modulo-Operator-Examples-09.png" data-lazy- height="387" src="data:image/svg xml,” width=”806″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/C-Modulo-Operator-Examples-10.png" data-lazy- height="127" src="data:image/svg xml,” width=”974″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/C-Modulo-Operator-Examples-11.png" data-lazy- height="25" src="data:image/svg xml,” width=”497″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/C-Modulo-Operator-Examples-12.png" data-lazy- height="474" src="data:image/svg xml,” width=”745″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/C-Modulo-Operator-Examples-13.png" data-lazy- height="110" src="data:image/svg xml,” width=”493″>

Example 05

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/C-Modulo-Operator-Examples-14.png" data-lazy- height="27" src="data:image/svg xml,” width=”516″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/C-Modulo-Operator-Examples-15.png" data-lazy- height="375" src="data:image/svg xml,” width=”916″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/C-Modulo-Operator-Examples-16.png" data-lazy- height="229" src="data:image/svg xml,” width=”487″>

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

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Author-Image-150×150.jpg" height="112" src="data:image/svg xml,” width=”112″>

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.