In this program, Take lower range and higher range from the user and display all prime numbers between a given range.
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.
<!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>
Input has been taken through the javascript prompt method
enter the lower bound: 5 enter the upper bound: 20 5 7 11 13 17 19
primeall()
function will get called.primeall()
function will print all prime numbers between a given rangefor(i=lb;i<=n;i++)
.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;
}
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
enter the lower bound: 10 enter the upper bound: 50 11 13 17 19 23 29 31 37 41 43 47