w3resource

JavaScript: Find the smallest round number that is not less than a given value

JavaScript Basic: Exercise-128 with Solution

Write a JavaScript program to find the smallest round number not less than a given value.

Note: A round number is informally considered to be an integer that ends with one or more zeros. So, 590 is rounder than 592, but 590 is less round than 600.

Pictorial Presentation:

JavaScript: Find the smallest round number that is not less than a given value.

Sample Solution:

JavaScript Code:

// Function to find the nearest round number
function nearest_round_number(num) {
    // Loop until the number is divisible by 10
    while (num % 10) {
        // Increment the number by 1 until it is divisible by 10
        num++;
    }
    // Return the nearest round number
    return num;
}

// Test cases
console.log(nearest_round_number(56)); // Output: 60
console.log(nearest_round_number(592)); // Output: 600

Sample Output:

60
600

Live Demo:

See the Pen javascript-basic-exercise-128 by w3resource (@w3resource) on CodePen.


Flowchart:

Flowchart: JavaScript - Find the smallest round number that is not less than a given value

ES6 Version:

// Function to find the nearest round number
const nearest_round_number = (num) => {
    // Loop until the number is divisible by 10
    while (num % 10) {
        // Increment the number by 1 until it is divisible by 10
        num++;
    }
    // Return the nearest round number
    return num;
};

// Test cases
console.log(nearest_round_number(56)); // Output: 60
console.log(nearest_round_number(592)); // Output: 600

Previous: JavaScript program to reverse the order of the bits in a given integer.
Next: JavaScript program to find the smallest prime number strictly greater than a given number.

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.