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

JS print all prime numbers

// program to print prime numbers between the two numbers

// take input from the user
const lowerNumber = parseInt(prompt('Enter lower number: '));
const higherNumber = parseInt(prompt('Enter higher number: '));

console.log(`The prime numbers between ${lowerNumber} and ${higherNumber} are:`);

// looping from lowerNumber to higherNumber
for (let i = lowerNumber; i <= higherNumber; i++) {
    let flag = 0;

    // looping through 2 to user input number
    for (let j = 2; j < i; j++) {
        if (i % j == 0) {
            flag = 1;
            break;
        }
    }

    // if number greater than 1 and not divisible by other numbers
    if (i > 1 && flag == 0) {
        console.log(i);
    }
}
Comment

JavaScript Prime Number

// To understand this let's understand that Prime number has only two factors 
// 1) first factor = number it self
// 2) second factor = 1
// for instance 3 has only factors 3*1 = 3 and 1;
// I have taken two for loop to get two different numbers once we get two factors then it will console i
function getPrime(until) {
  let factor;
  for (let i = 0; i < n; i++) {
    factor = 0;
    for (let j = 1; j <= n; j++) {
      if (i % j == 0) {
        factor++;
      }
    }
    if (factor == 2) console.log(i);
  }
}
getPrime(30);
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 :: How to get unix timestamp from tomorrow nodejs 
Javascript :: js scrollto 
Javascript :: valid phone number regex with country code 
Javascript :: retrieve object array value based on key 
Javascript :: jquery is triggered on every element with class name 
Javascript :: regex for lowercase letters js 
Javascript :: node load file 
Javascript :: shadowoffset react native constructor 
Javascript :: An accessor cannot be declared in an ambient context. 
Javascript :: express draft 
Javascript :: javascript restrict number to range 
Javascript :: get id button clicked react 
Javascript :: electron download 
Javascript :: js character certain count 
Javascript :: make js file windows command 
Javascript :: chart js y axis integer 
Javascript :: node js check if a user exists in mysql 
Javascript :: remove array empty values javascript 
Javascript :: datatable visable show entries 
Javascript :: js append class 
Javascript :: javascript get timestamp 
Javascript :: neo4j delete relationship nodes 
Javascript :: npm package for sorting in reactjs 
Javascript :: node js request download file 
Javascript :: how to format multiline string in javascript 
Javascript :: javascript iterate object 
Javascript :: javascript redirect to another url example 
Javascript :: javascript get x,y point on circle 
Javascript :: how to return character associated to character code javascript 
Javascript :: datepicker get selected date 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =