Swift 4 remainder operator (a%b) specifies how many multiples of the second operand (b) will fit inside first operand (a) and returns the value that is left over (known as the remainder).
Note: In other language, the remainder operator is known as modulo operator but in Swift 4, its behavior for negative numbers strictly specifies that it is a remainder rather than a modulo operation.
For example, to calculate 9%4, first find out how many 4s will fit inside 9.
There are two 4s inside 9 and 1 value is remaining. So, it will show 1 as output.
In Swift, it will be written as:
For a % b, the % operator calculates the following equation and returns remainder as output:
Here, multiplier is the largest number of multiples of b that will fit inside a.
Same method for negative value of a
The sign of b is ignored for negative values of b. This means that a % b and a % -b always give the same result.