Modulo using Recursion
Explore the concept of the modulo operation and discover how to implement it recursively in Java without relying on the built-in % operator. Learn to identify the base case and recursive case and see how the method calls itself until the remainder is found.
What is the modulo operation?
The modulo operation (abbreviated as mod) finds the remainder after the division of one number, the dividend, by another number, the divisor.
Below is an image that illustrates how to revise the concept of the remainder using a basic division example:
In the example above, we have divided 14, the dividend, by 4, the divisor, which results in 3 as the quotient and 2 as the remainder.
If we were to use a mod function, it would be implemented as follows:
We read this as 14 mod 4 is equal to 2.
💡 Note
If the dividend is fully divisible by the divisor, the remainder is
...