| by Arround The Web | No comments

C++ Modulus

In many programming languages, we often use an operator named “modulus” with the symbol representation as “%”. This modulus operator is used to find the remainder when the two numerical integers or numbers are divided by each other. This operator takes the two operands: dividend and divisor. What is left after their division is done is the remainder, X % Y = R; where X and Y are dividend and divisors respectively and R is the remainder. We use this remainder as a piece of information if we want to know whether or not the two dividing numbers are factors of each or not. We also use the remainder to let us know if the number is even or odd.

Procedure
There exists multiple uses of the modulus operator that we use for different functions in a programming language. We will perform modulus for different functions for different examples. Each example will brief us on the different use of the modulus operator. So, let’s dig into solving examples for “C++ Modulus Operator”.

Example # 01
In the first example, we will get familiar with the syntax of the modulus operator and will solve a simple example of the Modulus operator. For this purpose, we will apply the modulus operator on both the dividend and divisor having the same data types i.e., integer “int”. We will define the two variables, let’s say x & y as integers. Then, we will assign some random value to these integers. After the assignment of the value, we will apply the modulus operator on these two values as “dividend % divisor” and will store this in some other variable. Then, we will display that variable using the print function.

Output:

The remainder in the output has returned a value equal to zero. This means that the x was completely divisible by the y. Hence, x is the factor of y.

Example # 02
In this second example, we will learn how we can use the modulus operator in the chain to compute the modulus of more than two variables. First, we will define the data type of the variables. In this case, we will take three variables and will compute their chain modulus. Choose three variables at random e.g., x, y, z with the same data types as integers and initialize them by assigning the different values to each variable. Then, apply the modulus operator on these three variables as “x% y% z”. Display it using “cout <<”. This can be done by running the following code in the code editor:

Output:

Modulus of x % y that is 13 % 5 came out to be 3 and modulus (x % y) % z i.e. (3) % 2 is 1. This is the reason why our output came out to be exactly equal to one.

Example # 03
We have applied the modulus on the variable with the same data types or the data types whose combination is okay with the modulus operator. In this example, we will learn the restrictions of the operation of the modulus operator. The modulus operator does not work on the data types, float and double. To verify, let’s try an example where we will define the two variables with data type float and will apply the modulus on them. The results can be seen in the following output.

In the example when we used the float as the data type of the two variables “a” and “b” and assigned them the floating values e.g. 13.4 and 5.5 respectively. The modulus operator did not perform well on these two variables, and it had compilation errors pointing to data type float.

Example # 04
With the help of the modulus operator, we can also find out if the number is even or odd. We can use this function in applications where we want to check on certain odd and even values. To find the even number, we simply take the modulus of that number by 2. If the remainder comes out to be 1 or any number other than 0, the number is odd on the contrary. If the remainder comes out to be 0, the number is even. We have tried to implement this concept with the code given below:

Output:

The integer “a” was assigned the value 4 and taking its modulus with 2. The remainder resulted in zero which means that ‘a’ is an even number.

Example # 05
This example will show how we can make use of the mode operator modulus operator if we want to generate some integers that are less than the specific value or integer. We will use the rand function whose value will be then used by the modulus operator to pair with the desired upper limit of the specified maximum value. First, we will import all the important libraries as:

$ #include <iostream>
$ #include <vector>
$ #include <ctime>

Using namespace std, we will import vector, endl (to end statement), cout (to display), and cin. After this step, we will define the maximum limit, which in this example is 1000. Then, we will set how many numbers we want to generate that would be equal to 10. In the main, we will run the index up to the maximum limit and will generate the numbers using the rand function by pairing its returned value with the modulus of the maximum limit and will display the output.

Output:

The above code has generated the output where the ten numbers have been generated that are less than a thousand since we had defined the maximum limit of the numbers to be generated as less than a thousand and a total of ten in the numbers.

Conclusion

With the help of this guide, we can find out what exactly is modulus operator, what is its syntax, and how we can find the use of the modulus operator in various applications. We have solved different examples related to different uses of the modulus operator in C++ applications. Furthermore, we have learned about the restrictions of the modulus operator as well.

Share Button

Source: linuxhint.com

Leave a Reply