(C++) operator%

January 11, 2018 · View on GitHub

 

 

 

 

 

(C++) operator%

 

Operator to obtain the remainder from an integer division.

 


const int three = 7 % 4; //3 is the remainder of 7 divided by 4

 

operator% can often be found in prime determination (in the tool PrimeExpert for example).

 

The code below shows how to determine if a value is odd or even.

 


if (x%2==0) {   //x must be even } else {   //x must be odd }

 

The STL functor encapsulating operator% is called std::modulus.