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

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
http://www.code-sample.com
http://www.code-sample.xyz

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.