Modulo is a mathematical operation that finds the remainder when an integer is divided by another. It is frequently expressed as mod or symbolized as % , therefore, the expression 5 mod 3 can also be written as 5 % 3 , where 5 is the dividend and 3 is the divisor.
Given two positive numbers, a and n, a modulo n (abbreviated as a mod n) is the remainder of the division of a by n, where a is the dividend and n is the divisor.
For example, the expression 9 % 2 would evaluate to 1 because 9 divided by 2 has a quotient of 4 and a remainder of 1.
The expression 10 % 2 would evaluate to 0 because 10 divided by 2 has a quotient of 5 and a remainder of 0.
Practical Uses of Modulo
To check if the year, y, is a leap year, you use the expression, y % 4 . The division of a leap year by 4 has 0 remainder, therefore, y % 4 should evaluate to 0 if y is a leap year. The example below shows a function to check if a given year is a leap year.
A card pack contains 55 cards to be shared among 4 players. Each player must have a maximum of 5 cards on his tray, which means, 20 cards would be given out from the card pack. The example below shows a function that does the card sharing.
Check Multiples of a Number
An even number is known as a number that can be divided by 2 without having any remainder (having 0 remainder). The expression, 4 % 2 evaluates to 0 because 4 is an even number, while the expression 9 % 2 evaluates to 1 because 9 is an odd number. Therefore, 4 is a multiple of 2, but 9 is not a multiple of 2.
The expression, 25 % 5 evaluates to 0 because 25 is a multiple of 5, while the expression 28 % 5 evaluates to 3 because 28 divided 5 has a remainder of 3, therefore, 28 is not a multiple of 5.
Modulo operator can also be used in time calculations.
Annabel visited her friend at 2:00pm (14:00). She left her friends place after spending 30hrs with her. At what time of the day did she leave her friends place?
Christian Ezeani is a web developer, software engineer, and project manager at Cyberpull Technologies and TeamCYB. My interests range from technology to writing.