Skip to content
Related Articles

Related Articles

Improve Article

Round off a number to the next multiple of 5 using JavaScript

  • Difficulty Level : Easy
  • Last Updated : 27 Aug, 2019

Given a positive integer n and the task is to round the number to the next whole number having divisible by 5.

Examples:

Input : 46 
Output : 50

Input : 21
Output : 25

Input : 30 
Output : 30

Approach 1:

  • Take the number in a variable.
  • Divide it by 5 and get the decimal value.
  • Take the ceil value of the decimal value by using math.ceil().
  • Multiply it by 5 to get the result.




<script>
    function round(x) {
        return Math.ceil(x / 5) * 5;
    }
  
var n = 34;
console.log(round(n)); 
</script>

Output:

35

Approach 2:

  • Take the number in a variable.
  • If it is divisible by 5, return the same number.
  • Else divide it by 5, take floor value and again multiply it by 5 and add 5 as well.




<script>
    function round(x) {
        if (x % 5 == 0) {
            return int(Math.floor(x / 5)) * 5;
        } else {
            return (int(Math.floor(x / 5)) * 5) + 5;
        }
    }
  
var n = 34;
console.log(round(n)); 
</script>

Output:

35
My Personal Notes arrow_drop_up
Recommended Articles
Page :