“JavaScript Temukan bilangan prima” Kode Jawaban

prima atau tidak di javascript

function isPrime(num) {
  for(var i = 2; i < num; i++)
    if(num % i === 0) return false;
  return num > 1;
}
Troubled Trout

bilangan prima javascript

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

JS mencetak semua bilangan prima

// 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);
    }
}
Jealous Jackal

JavaScript Temukan bilangan prima

cosnole.log("hi karthick")
karthick s

Jawaban yang mirip dengan “JavaScript Temukan bilangan prima”

Pertanyaan yang mirip dengan “JavaScript Temukan bilangan prima”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya