Certainly! Below is an example JavaScript program that prints all prime numbers between two given intervals:
function printPrimesBetweenIntervals(start, end) {
// Function to check if a number is prime
function isPrime(num) {
if (num <= 1) return false;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return false;
}
}
return true;
}
// Validate input
if (start >= end) {
console.log("Invalid input: Start should be less than end.");
return;
}
// Print prime numbers in the given interval
console.log(`Prime numbers between ${start} and ${end}:`);
for (let i = start; i <= end; i++) {
if (isPrime(i)) {
console.log(i);
}
}
}
// Example usage: Print prime numbers between 10 and 50
printPrimesBetweenIntervals(10, 50);
This program defines a function printPrimesBetweenIntervals that takes two parameters,
start and end, representing the interval. The function uses an inner function
isPrime to check if a number is prime. It then iterates through the numbers in the given interval, calling the
isPrime function to determine if each number is prime, and prints the prime numbers.
You can customize the example usage by providing different values for
start and end to find prime numbers within the desired interval.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Certainly! Below is an example JavaScript program that prints all prime numbers between two given intervals:
This program defines a function printPrimesBetweenIntervals that takes two parameters, start and end, representing the interval. The function uses an inner function isPrime to check if a number is prime. It then iterates through the numbers in the given interval, calling the isPrime function to determine if each number is prime, and prints the prime numbers.
You can customize the example usage by providing different values for start and end to find prime numbers within the desired interval.