“cetak bilangan prima pertama di javascript” Kode Jawaban

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

cetak bilangan prima pertama di javascript

// first n prime numbers (divisble by: 1 and themselves only e.g. 2,3,5,7,11)
    const prime = (n) => {
      let arr = [];
      for (let i = 2; arr.length < n; i++) {
        var flag = 0;
        for (let k = 2; k <= i; k++) {
          if (i % k == 0 && i > k) {
            flag = 1; //if i is divisible by any small number than it is not prime number
          } else if (i % k == 0 && flag != 1) {
            arr.push(i);
          }
        }
      }
      return arr;
    };
    console.log(prime(5));
Bade_Mia

Jawaban yang mirip dengan “cetak bilangan prima pertama di javascript”

Pertanyaan yang mirip dengan “cetak bilangan prima pertama di javascript”

Lebih banyak jawaban terkait untuk “cetak bilangan prima pertama di javascript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya