Skip to content

Modulo

  • Modulo operation finds the remainder when one number is divided by another
c
int a = 5, b = 3, result;
result = a % b;
c++
int a = 5, b = 3, result;
result = a % b;
java
int a = 5, b = 3, result;
result = a % b;
python
a, b = 5, 3
result = a % b

Example

Mr. Ali sells 153 cakes. He wants to pack these cakes into bundles of 8 cakes per bundle. How many cakes will remain unpacked?

Pseudocode

txt
BEGIN
    INITIALIZE totalCakes as 153
    INITIALIZE cakesPerBundle as 8

    COMPUTE remainingCakes = totalCakes % cakesPerBundle

    DISPLAY "Number of cakes remaining unpacked: ", remainingCakes
END
c
int totalCakes = 153;
int cakesPerBundle = 8;
int remainingCakes = totalCakes % cakesPerBundle;

printf("Number of cakes remaining unpacked: %d\n", remainingCakes);
c++
int totalCakes = 153;
int cakesPerBundle = 8;
int remainingCakes = totalCakes % cakesPerBundle;

cout << "Number of cakes remaining unpacked: " << remainingCakes << endl;
java
int totalCakes = 153;
int cakesPerBundle = 8;
int remainingCakes = totalCakes % cakesPerBundle;

System.out.println("Number of cakes remaining unpacked: " + remainingCakes);
python
total_cakes = 153
cakes_per_bundle = 8
remaining_cakes = total_cakes % cakes_per_bundle

print("Number of cakes remaining unpacked: {}".format(remaining_cakes))