Modulus operator is used as an integrated method. It deals with the leftovers which we get by doing some numeral division. It is written like “%” this. It is a kind of formula which helps us in writing our mathematical problems. For instance, we have to divide 13 by 4. The result will be 1 as a residue. It is difficult to perform this method in C++. Modulus operator gives us a way to execute this method in programming.
This is one of the main functions of modulus operator. But there are many other important tasks which can be carried out by this operator. Mostly, it serves us in getting a number which is haphazardly generated. It makes that number shorter. You can even get to know through this operator about different numbers, whether they are factor of others or not.
You can get an idea about even or odd nature of a number with the help o modulus operator. It divides the number by two. If the answer is zero, then the number is even. Otherwise, it will be called odd.
using namespace std;
cin >> num;
// calculates the residue
if ( num % 2 == 0 )
cout << num << " is even ";
The main function is num%2= =0. Here, we will get an even number if it will be completely divided by 2. If no residue is left, then that specific number can be used again for division.
What will be your method of checking a prime number by modulus operator?