w3resource

JavaScript: Round up an integer value to the next multiple of 5

JavaScript Math: Exercise-28 with Solution

Round Integer to Next Multiple of 5

Write a JavaScript function to round up an integer value to the next multiple of 5.

Test Data:
console.log(int_round5(32));
35
console.log(int_round5(137));
140

Sample Solution:

JavaScript Code:

// Define a function named int_round5 that rounds a number up to the nearest multiple of 5.
function int_round5(num)
{
    // Divide the number by 5, round the result up to the nearest integer, then multiply by 5.
    return Math.ceil(num/5)*5;
}

// Output the result of rounding 32 up to the nearest multiple of 5 to the console.
console.log(int_round5(32));
// Output the result of rounding 137 up to the nearest multiple of 5 to the console.
console.log(int_round5(137));
// Output the result of rounding 142 up to the nearest multiple of 5 to the console.
console.log(int_round5(142));

Output:

35
140
145

Visual Presentation:

JavaScript: Math - Round up an integer value to the next multiple of 5.

Flowchart:

Flowchart: JavaScript Math- Round up an integer value to the next multiple of 5

Live Demo:


For more Practice: Solve these Related Problems:

  • Write a JavaScript function that rounds an integer up to the next multiple of 5 using arithmetic operations.
  • Write a JavaScript function that computes the next multiple of 5 for a given integer and handles negative numbers appropriately.
  • Write a JavaScript function that rounds a number to the nearest multiple of 5, biasing the result to always round up.
  • Write a JavaScript function that calculates the remainder of a number when divided by 5 and adjusts the value to the next multiple.

Go to:


PREV : Calculate Degrees Between Two Points.
NEXT : Convert Positive Number to Negative.

Improve this sample solution and post your code through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.