Prime Numbers using JavaScript

Prime Numbers using JavaScript

What is Prime number?

A prime number can be divided only by itself or by 1 that number is called prime number.

For Example : The number 7 can be divided only by 7 or by 1 that means we say that the number 7 is a prime member.

Noted point:  The number 0 and 1 are not considered prime numbers.

The live demo example link https://jsfiddle.net/xfpe35tb/

JavaScript Program code

function isPrime(number) {

    d = number - 1;

    // If n is less than 2 or not an integer then we can say not a prime number.
    if (number < 2) {
        alert('If n is less than 2 or not an integer then we can say not a prime number.');
        return false
    }

    if (number != Math.round(number)) {
        alert('If n is less than 2 or not an integer then we can say not a prime number.');
        return false
    }

    while (d > 1) {
        if ((number % d) == 0) {
            return false;
        }
        d--;
    }
    return true;
}

if (isPrime(1)) {
    alert('Prime number');
}
else {
    alert('Not Prime number');
}


//The Output/Result: if input number is 1 or 0.

'If n is less than 2 or not an integer then we can say not a prime number.

Other than 0 and 1 will display is prime number or not prime number.


ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.