JavaScript Program to Print All Prime Numbers in an Interval

To understand this example, you should have the knowledge of the following JavaScript programming topics:


A prime number is a positive integer that is only divisible by 1 and itself. For example, 2, 3, 5, 7, 11 are the first few prime numbers.

For example, 4 is not a prime number because it is divisible by 1, 2 and 4 itself. It is a composite number.

Example: Print 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);
    }
}

Output

Enter lower number: 2
Enter higher number : 10
The prime numbers between 2 and 10 are:
2
3
5
7

In the above program, the user is prompted to enter lower and higher bound numbers. Then the prime number between those numbers (including the lower and higher bounds, if any) are listed out.

Two nested for loops are used in the above program.

  • The first for loop is used to loop between the numbers provided by the user. In this case, from 2 to 10.
  • A variable flag is set to 0.
  • The second for loop is used to loop between 2 to the number that is stored in i.
  • Inside the second loop, the value of i is divided by each number from 2 to value one less than i (i - 1).
  • While dividing, if any number remainder results in 0, that number is not a prime number. So the variable flag is set to 1.
  • Finally, all the numbers that have a flag 0 (not divisible by other numbers) are printed.

Also Read:

Before we wrap up, let’s put your knowledge of JavaScript Program to Print All Prime Numbers in an Interval to the test! Can you solve the following challenge?

Challenge:

Write a function to find all prime numbers within a given range.

  • A number is considered as a prime number if it has only two factors: 1 and the number itself.
  • Given two integers, start and end, return a list of prime numbers from start (inclusive) to end (inclusive).
  • For example, if start = 10 and end = 20, the expected output is [11, 13, 17, 19].
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges