Trusted answers to developer questions

Gutha Vamsi Krishna

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`

.

- Take the given variable
`i`

. - Divide variable
`i`

with integer`5`

. - Calculate the
`ceil`

of the above result. - Multiple it with
`5`

. - It will give the next whole number divisible by
`5`

after variable`i`

.

//given number let i = 47; //calculate next whole number divisible by 5 let nextNum = Math.ceil(i/5) * 5; //print next number console.log(nextNum);

In the above code snippet:

- In line
`2`

: We initialize integer`i`

with value`47`

. - In line
`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`

. - In line
`8`

: We print variable`nextNum`

to the console which is the next whole number divisible by`5`

after the given number`47`

.

//given number let i = 24; //calculate next whole number divisible by 5 let nextNum = Math.ceil(i/5) * 5; //print next number console.log(nextNum);

In the above code snippet:

- In line
`2`

: We initialize integer`i`

with value`24`

. - In line
`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`

. - In line
`8`

: We print the variable`nextNum`

to the console, which is the next whole number divisible by`5`

after the given number`24`

.

RELATED TAGS

communitycreator

javascript

CONTRIBUTOR

Gutha Vamsi Krishna

RELATED COURSES

View all Courses

Keep Exploring

Related Courses