Table of Contents
show
Example: 2A
Write a JavaScript to display whether a number is prime or not in alert box
Script
<html>
<head>
<title>PRIME NUMBER CHECKER</title>
</head>
<body>
<script type = "text/javascript">
var num = prompt("Type number here");
num = parseInt(num);
var b;
var flag = 1;
if(num <= 1)
{
flag = 0;
}
else
{
for(i=2;i<num;i++)
{
b = num % i;
if(b == 0)
{
flag = 0;
break;
}
}
}
if(flag == 0)
alert(num+" is not a prime number");
else
alert(num+" is a prime number");
</script>
</body>
</html>
Output
Views: 11