In this shot, we will learn how to round off a number to the next multiple of 5 using JavaScript.
We will start with the problem statement, followed by an example.
Given a positive integer i
, round i
to the next whole number divisible by 5
.
Input: 47
Output: 50
Explanation: 47
is not divisible by 5
, and the next whole number divisible by 5
after 47
is 50
.
Input: 24
Output: 25
Explanation: 24
is not divisible by 5
, and the next whole number divisible by 5
after 24
is 25
.
i
.i
with integer 5
.ceil
of the above result.5
.5
after variable i
.//given numberlet i = 47;//calculate next whole number divisible by 5let nextNum = Math.ceil(i/5) * 5;//print next numberconsole.log(nextNum);
In the above code snippet:
2
: We initialize integer i
with value 47
.5
: We divide i
with 5
and take the ceil
of it. Then, we multiply it with 5
and assign it to the variable nextNum
.8
: We print variable nextNum
to the console which is the next whole number divisible by 5
after the given number 47
.//given numberlet i = 24;//calculate next whole number divisible by 5let nextNum = Math.ceil(i/5) * 5;//print next numberconsole.log(nextNum);
In the above code snippet:
2
: We initialize integer i
with value 24
.5
: We divide i
with 5
and take the ceil
of it. Then, we multiply it with 5
and assign it to the variable nextNum
.8
: We print the variable nextNum
to the console, which is the next whole number divisible by 5
after the given number 24
.