Modulo division is an algorithm for finding the remainder of dividing the first natural number by the second.
% – modulo division. This is the operation of taking the residue modulo (calculating the remainder of division).
The result of this operation is the remainder of integer division; for example, if we divide 11 by 3, we get 3 whole parts (since 3*3=9), and the remainder will be 2; this number will be the result of the modulo division, an example for the C++ language:
11/3 = 3 whole, 2 remainder. i.e., 11-3*3=2
11%3 = 2 (remainder)
27%23 = 1 whole, 4 remainder. i.e., 27-1*23=4
Note:
- The modulo division operation can only be applied to integer data. Attempts to violate this rule will result in a compilation error.
- If a smaller number is divided by a larger one using %, the result will be the smaller number itself: 3%10 = 3
- Modulo division by zero is not allowed; this will lead to incorrect program behavior at runtime.
See also:
- PDF File Modulo Division: Theory and Practice
- C++ Operators
- Setting the range for the rand random number generator
1. Example in C++
In C++, the % operator works only with integer types (int, char, long). To work with floating-point numbers, use the fmod function from the <cmath> library.
#include <iostream>
int main() {
int a = 11;
int b = 3;
// Classic modulo division
int result = a % b;
std::cout << a << " % " << b << " = " << result << std::endl; // Вывод: 2
// Handling the case where the smaller number is divided by the larger one
std::cout << "3 % 10 = " << (3 % 10) << std::endl; // Вывод: 3
// Important: division by 0 will cause a runtime error
/* int zero = 0;
int error = a % zero;
*/
return 0;
}
2. Example in PHP
In PHP, the % operator converts both operands to integers before performing the calculation. If you need to find the remainder of a division involving fractional numbers, use the fmod() function.
<?php
// Integer division
$a = 11;
$b = 3;
echo $a % $b; // Outputs: 2
// Floating-point operands (will be converted to int)
echo 11.5 % 3.2; // Выведет: 2 (как 11 % 3)
// For fractional numbers, use fmod
echo fmod(11.5, 3.2); // Выведет: 1.9
?>
3. WordPress shortcode
For WordPress, you can add a simple shortcode to your theme’s functions.php file. This will allow you to dynamically display the result of a division directly within the text of your posts using the syntax [modulo a=”11″ b=”3″].
This code fully meets your requirements: it’s lightweight, uses native PHP, and doesn’t require installing any plugins.
/**
* Shortcode for calculating the remainder of a division (modulo)
* Usage: [modulo a="11" b="3"]
*/
add_shortcode('modulo', function($atts) {
// Extract parameters and convert to integers
$params = shortcode_atts([
'a' => 0,
'b' => 1,
], $atts);
$a = (int)$params['a'];
$b = (int)$params['b'];
// Check for division by zero
if ($b === 0) {
return '<span style="color:red;">Error: Division by zero</span>';
}
return $a % $b;
}); 


