JavaScript Program to Display all Prime Number for given Range

In this program, Take lower range and higher range from the user and display all prime numbers between a given range.

what is a prime number?

a prime number is a number that will divide by 1 or itself. prime number from 1 to 50 are 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47.

Print all prime numbers between given range using for loop

<!DOCTYPE html>
  <html>
  <body>
     <script>
       function primeall()
       {
        var j,b;
        var lb=parseInt(prompt("enter the lower bound:"));
        var n=parseInt(prompt("enter the upper bound:"));
        for(i=lb;i<=n;i++)
        {
              b=0;
              for(j=1;j<=i;j++)
              {
               if(i%j==0)
               {
                 b=b+1;
               }
              }
              if(b==2)
              {
                document.write(i+"</br>");
          }
          }
       }
      </script>
      <form>
           <input type="button" value="prime number" onclick="primeall();">
      </form>
  </body>
  </html>
  
Run now

Input has been taken through the javascript prompt method

output

enter the lower bound: 5
  enter the upper bound: 20
  5
  7
  11
  13
  17
  19

Working of print all prime numbers between a given range

  • In this program, when you click on 'prime number' button, primeall() function will get called.
  • primeall() function will print all prime numbers between a given range
  • In this program, you will ask a user to enter lower and upper-bound, after getting input iterate for the loop from lower-bound(lb) to upper-bound(up), for(i=lb;i<=n;i++).
  • In the inner loop, you will iterate from 1 to current iteration of the outer loop i.e for(j=1;j<=i;j++). In the inner loop, you will check whether the number is prime or not with the help of the if statement i.e
    if( i%j==0 )
        {
             b = b + 1;
        }
  • If the value of b becomes 2 during an iteration of the inner loop from 1 to i (current iteration of the outer loop) that means a number is prime, print the prime number and go for the next iteration of the outer loop, this process will continue until the outer loop break.

Display all Prime Number for given Range using while loop

In this program, you will take lower and upper ranges by using the javascript prompt method and print all prime numbers between a given range using a while loop.

<!DOCTYPE html>
  <html>
  <body>
    <script>
       function primeall()
      {
	    var j,b;
    	    var lb=parseInt(prompt("enter the lower bound"));
	    var ub=parseInt(prompt("enter the upper bound"));
	    while(lb<=ub)
	   {
              b=0;
	      j=1;
	      while(j<=lb)
              {
                  if(lb%j==0)
                  {
                    b=b+1;
                   }
	           j++;
               }
               if(b==2)
               {
                  document.write(lb+"</br>");
	        }
	       lb++;
            }
       }
      </script>
      <form>
           <input type="button" value="prime number" onclick="primeall();">
      </form>
  </body>
  </html>
  

Input has been taken through the javascript prompt method

output

enter the lower bound: 10
enter the upper bound: 50
11
13
17
19
23
29
31
37
41
43
47