Q:

Write a JavaScript program to find the smallest prime number strictly greater than a given number

0

Write a JavaScript program to find the smallest prime number strictly greater than a given number

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

function next_Prime_num(num) {
    for (var i = num + 1;; i++) {
        var isPrime = true;
        for (var d = 2; d * d <= i; d++) {
            if (i % d === 0) {
                isPrime = false;
                break;
            }
        }
        if (isPrime) {
            return i;
        }
    }
}

console.log(next_Prime_num(3));
console.log(next_Prime_num(17));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now