Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

prime numbers javascript

function isPrime(num) {
    for (let i = 2; i * i <= num; i++)
        if (num % i === 0)
          return false; 
    return num > 1;
}
Comment

prime number in javascript

// program to check if a number is prime or not

// take input from the user
const number = parseInt(prompt("Enter a positive number: "));
let isPrime = true;

// check if number is equal to 1
if (number === 1) {
    console.log("1 is neither prime nor composite number.");
}

// check if number is greater than 1
else if (number > 1) {

    // looping through 2 to number-1
    for (let i = 2; i < number; i++) {
        if (number % i == 0) {
            isPrime = false;
            break;
        }
    }

    if (isPrime) {
        console.log(`${number} is a prime number`);
    } else {
        console.log(`${number} is a not prime number`);
    }
}

// check if number is less than 1
else {
    console.log("The number is not a prime number.");
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript indentation 
Javascript :: how to remove first character from string in javascript 
Javascript :: javascript is url 
Javascript :: jquery use variable in string 
Javascript :: largest and smallest number in an array 1-100 javascript 
Javascript :: create and fill array javascript 
Javascript :: check array values equal js 
Javascript :: new variable in loop javascript 
Javascript :: react router route not found redirect 
Javascript :: how to filter an array by list of objects in javascript 
Javascript :: async await class component react 
Javascript :: round innerhtml up 
Javascript :: li dots 
Javascript :: react declare multiple states 
Javascript :: axios.interceptors.response.use 
Javascript :: redirect to download javascript 
Javascript :: js sort object properties alphabetically 
Javascript :: js array.prototype.join 
Javascript :: how to show selected value in select box using jquery 
Javascript :: install php7 runtime brackets 
Javascript :: js merge 2 form data 
Javascript :: javascript send post data with ajax 
Javascript :: javascript function uppercase to lowercase 
Javascript :: how to go back one directory in git bash 
Javascript :: router in next js 
Javascript :: how to add a picker in expo 
Javascript :: react native password 
Javascript :: crud template 
Javascript :: fs.readdir callback function 
Javascript :: express cors specific origins 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =