Let’s begin this article with the system update. Start your terminal console of Ubuntu 20.04 using “Ctrl+Alt+T” on your desktop screen. The console application called “terminal” will be launched on your screen, and you will be ready to use some commands on it. We will be starting our first command execution on it with a system update. We are utilizing the “apt-get” package to do so with the “sudo” rights. The keyword “update” is a must-have in this instruction. On executing this instruction, the system wouldn’t allow us to execute it until we enter our admin account password. So, we added our password, and the system update started.
After our Ubuntu 20.04 has installed its updates, we need to make a new file to work in C#. The use of Ubuntu’s “touch” instruction would help us here. We have used the “touch” basic instruction to create a file with the “.cs” extension named “mod.cs”. The basic list command “ls” has been utilized to display the home folder contents. The newly created C# file has been listed here. You need to open the file explorer, right-click on the file “new.sh” and tap on the option “Open in text editor” to open this file in the text editor to create some C# code. The empty file will be launched on your screen.
Example # 01
Starting from the basic example, we will be initializing the system namespace on the first line using the keyword “using”. This is the standard namespace that must be utilized in the code to use some basic functions of C# in the program. We have started a public class “Test” and initialized a main() driver function having a static void return type. Within this main method, we have initialized two integer variables, “x” and “y”, with numerical values “10” and “7”. We have been using the WriteLine() method of the class Console from namespace “System” to display the modulus of “x” with “y”. The “dot” product has been used to call this function. The modulus of “x” with “y” has been calculated and displayed. The “x” variable will be the numerator, and “y” will be the denominator. Ensure to use the correct syntax for using the “WriteLine” function by separating simple text with the variables or calculations by “+”.
Now, the program is complete and ready for use. Save it with Ctrl+S and quit the editor. Coming back to the shell, use the “mcs” compiler of C# to compile this code first. This will create an “exe” file for this file on the same current directory. We will be using this “exe” file in the shell with a “mono” runtime executor to run the code. Using the “mono” instruction, we have the modulus calculated by a program, i.e., 10%7 = 3.
Example # 02
We have elaborated a most basic program in the previous example. Now, we will be using the “for” loop to calculate the modulus of values from 1 to 10 with the value “5”. So, we have been using the same code with a minor change in its main driver program. We have initialized a “for” loop from 1 to 10. On each iteration, the value of “I” will be incremented, and the WriteLine() function of Console class from namespace “System” has been used to simply calculate and display the modulus of iteration number “I” with “5”.
We have saved the updated program and compiled it with the “mcs” C# compiler. No errors were returned. So, we have executed the “exe” file taken from this compilation to run our error-free code. The output has been displayed on the terminal screen. The modulus of each iteration number with the value “5” was calculated and displayed starting from 1 to 10. The following image shows the values of the modulus as 0, 1, 2, 3, and 4:
Example # 03
Let’s change the program slightly and use the “if” statement within our C# code. We have been using the “for” loop starting from 1 and ending at 20. On each iteration, the value of “I” will be incremented by 2. On each iteration, the “if” statement will check whether the modulus of iterator I with the value “8” is not equal to 1. If the modulus doesn’t equal 1, the WriteLine() function of the “Console” class will continue to calculate and display the modulus of “I” with 8 on the screen. If the value is equal to 1, the modulus of “i” with “8” will neither be calculated nor be displayed on the shell.
Upon compiling the original file mod.cs and executing the “exe” file with mono-runtime, we have the following result. You can see that the result of iteration numbers “9” and “17” has been missing from the output as it is equal to 1.
Example # 04
Let’s have our last example to see the basic illustration of using the “%” sign to calculate the modulus of two values. Thus, we have utilized a total of three “WriteLine()” function statements in the main() method. These statements use the WriteLine() function to calculate the modulus of two values where each value is different from the other statement value. It is a direct method to find out the modulus of two values.
The modulus of values for three statements has been calculated and displayed on the shell after using the mcs compiler and mono-runtime execution instruction.
Within the following updated code, we declared three variables x, y, and z. The modulus for x and y has been calculated first, and the modulus of variable z has been calculated using modulus values of “x” and “y”. The WriteLine() function of the Console class displayed the modulus of x, y, and z separately. Let’s compile the following code after saving it:
So, we have compiled the code file and executed its exe file with “mcs” and “mono” instructions, respectively. The modulus value of all three variables has been presented, as shown below:
Conclusion
Similar to simple algebraic mathematics, we have seen the simple methods of C# to calculate the modulus of two random values. In the introduction, we defined that the remainder is said to be a modulus of division. After, we have discussed examples with “for” loops, “if” statements, and direct use of the “%” character to calculate the modulus in the C# program. All four examples are comprehended in quite simple words and using the screenshot illustrations of codes. You can modify the program codes according to your choice. We hope you found this article helpful. Check the other Linux Hint articles for more tips and tutorials.