JavaScript

Function to check if a number is prime in Javascript

A prime number is a number only divisible by 1 or by itself. For example, 17 is only divisible by 17 or by itself. Thus 2, 3, 5, 7, 11, 13, 17…. are prime numbers.

Note: 0 and 1 are not prime numbers. 2 is the only prime and even number.
 

Program to check if the number is prime or not
function isPrime(nbr) {
  for(var i = 2; i < nbr; i++)
    if(nbr%i === 0) return false;
  return nbr > 1;
}

console.log(isPrime(2));

Output:

true
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *