Algorithm


  1. Input:

    • Accept the lower and upper bounds of the interval as input.
  2. Loop through the interval:

    • Initialize a loop to iterate through numbers from the lower bound to the upper bound.
  3. Check for primality:

    • For each number in the interval, check whether it is a prime number or not.
  4. Prime number check:

    • To check if a number is prime, iterate from 2 to the square root of the number.
    • If the number is divisible by any other number in this range, it is not prime. Otherwise, it is prime.
  5. Print prime numbers:

    • If a number is prime, print it.

 

Code Examples

#1 Code Example- Print Prime Numbers

Code - Javascript Programming

// 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);
    }
}
Copy The Code & Try With Live Editor

Output

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

Demonstration


JavaScript Programing Example to Print All Prime Numbers in an Interval-DevsEnv

Previous
JavaScript Practice Example #3 - Assign 3 Variables and Print Good Way