python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
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

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions