Let us get began by logging in from the Ubuntu 20.04 Linux program and opening the terminal shell by way of the exercise bar or a shortcut critical Ctrl+Alt+T. Make positive that your system need to have GCC compiler configured on it in advance of heading even further. If not, install it through the below query. Following that, you are fantastic to go with the examples of Modulo operator in C language.
Let’s choose a closer appear at some illustrations of modulo operators in the C programming language. As the terminal has been opened, you ought to build a C-sort file to publish some C language code in it. In Linux, the “touch” command is widespread in the development of this kind of sort information. That’s why, we have used it In our implementation and developed a new file named “new.c”.
You can uncover your recently designed file in the Linux home directory by File Manager. When utilizing the terminal, we can also open up the newly made file by just typing a straightforward command as under. This will open up it in a GNU nano editor utilized to edit this sort of sort of information.
Now the file has been opened in the GNU editor you can effortlessly style any code in it. Hence, we have extra a underneath simple code in it. This code consists of a header file for typical input and output, and then the principal approach has been outlined. In the major way, we have simply just set a printf statement to estimate the modulus of two random figures working with the percentage operator in just them and print them. The primary method ended. You can help save the file with “Ctrl+S” and close it with “Ctrl+X” to come back to the terminal shell.
Right after saving this file, compile it with the “gcc” compiler in the shell. The compiling of a file demonstrates no mistake this suggests the code is logically and syntactically accurate. Immediately after that, execute your file with the “a.out” instruction. The output displays the modulus “8” of two figures, “8” and “17”.
Case in point 02
Our initial case in point was a easy and to-the-level calculation of modulus by two random numbers. Let us just take an additional case in point to see the strategy of modulus. In this case in point, we will be making use of the variables to obtain out the modulus. Consequently, open the file “new.c” with the nano editor as per the subsequent command:
The file is opened now. Update your file with the beneath-shown code. This code has a header file and the primary purpose. The major method includes 3 integer-variety variables described at the start of it. Soon after that, we have assigned values to two of the variables, “a” and “b.” Then we have calculated the modulus of each the variables and assigned the approximated modulus benefit to a third variable which is “z” in our scenario. Then a printf assertion has been utilized to print the modulus value saved in the variable “z.” Then we have yet again calculated the modulus of the two the variables “a” and “b” by altering their positions this time. Once more printed the calculated modulus saved in the variable “z.” Immediately after this, we have assigned new values to both of those the variables “a” and “b.” Then we have calculated a new modulus of equally the freshly assigned variables yet again and print them. At previous, the major system has been shut, and we have saved the file once again with Ctrl+S. Jump back to the terminal making use of Ctrl+X.
Now compile the previously mentioned code with the gcc compiler and then execute the file. The output is specified beneath. We can see the three effects created by the 3-time calculated modulus in our terminal.
This time, we will verify no matter if the modulus benefits in the same on each facts sort or a little something new. So open up your file as soon as yet again as follows:
Now the file is opened in the GNU editor as under. Create out the under code in it. This time we have utilised the exact same normal header file and the key functionality in a C script. But the change is in the knowledge form of variables that have been declared in the code. We have used the float facts sort to uncover out the modulus and assigned float values to variables “a” and “b.” Then we have utilized the 3rd variable, “z” to help you save the worth of a modulus resulted from both equally the variables. Printf statement is being utilized to print out the modulus in the terminal. The perform finishes listed here. Conserve the code and quit the file by Ctrl+S and Ctrl+X consequently.
On compiling the earlier mentioned C-form file, we have obtained an error stating that we have used the invalid operator on float style information. This indicates we are not able to work out the modulus of float type details. So to calculate modulus, we need to give the integer style details.
After checking the valid knowledge sort for calculating modulus, let’s search at destructive integer sort variables. In this case in point, we will calculate the modulus of detrimental integer details sorts. Consequently, open up the code file all over again.
Now the file is opened, update it with the underneath-revealed C script, and save it through the “Ctrl+S” key. The total code is the very same, but we have outlined one particular damaging integer and 1 constructive integer this time. We have also calculated the modulus two moments in this instance by transforming the values of the variables “a” and “b.” Print statement has been utilised to show the modulus calculated by each variables and saved in the “z” variable. Exit the file by means of Ctrl+X.
Compilation and then executing a code have provided us modulus output in a unfavorable and good price.
Let us have an case in point of using modulus from array variety values. Pen the file to do so.
In the described code, we have defined an integer type array with 6 integer values. Then we have utilised for loop to print and work out the modulus of each benefit with the selection 5.
The output offers us the 6 output modulus of 6 array integer numbers.
At final, we have carried out with all the straightforward and most clear-cut examples of calculating modulus in C language. I hope you come across this post valuable and uncomplicated to use.