I'm trying to design a function int smallestDivisibleAfter(int number, int divisor)
such that it returns the smallest number greater than or equal to number
that is divisible by divisor
(which is non-zero) . Here all inputs and outputs are assumed to be non-negative.
Examples:
smallestDivisibleAfter(9,4); // Returns 12
smallestDivisibleAfter(16,2); // Returns 16
I came up with the code number + divisor - number % divisor
. However this ceases to work when number % divisor == 0
, since then smallestDivisibleAfter(16,2); // Returns 18
instead of 16
.
In addition, number - 1 + divisor - (number - 1)% divisor
does not work since int
will be replaced by unsigned long long
when I put this code into action.
What is the best solution here?