---
title: "Javascript program to print all prime number between two intervals"  
description: "Javascript program to print all prime number between two intervals"  
author: "Amartya Singh"  
published: 2023-02-16  
updated: 2023-11-26  
canonical: https://www.mindstick.com/forum/157407/javascript-program-to-print-all-prime-number-between-two-intervals  
category: "javascript"  
tags: ["jquery", "javascript", "scripting language", "programs"]  
reading_time: 2 minutes  

---

# Javascript program to print all prime number between two intervals

Find all the prime numbers between two intervals [input](https://www.mindstick.com/forum/159209/how-can-i-read-convert-an-input-stream-into-a-string-in-java) by the [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) using a [javascript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) program.

## Replies

### Reply by Aryan Kumar

Certainly! Below is an example JavaScript program that prints all prime numbers between two given intervals:

```plaintext
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.


---

Original Source: https://www.mindstick.com/forum/157407/javascript-program-to-print-all-prime-number-between-two-intervals

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
