0
   

check whether the user entered number is prime or not using javascript

 
 
Reply Tue 29 Aug, 2017 03:41 am
my code is:
<html>
<head>

<script type="text/javascript">
function prime()
{
var num= document.getElementById("myForm");

var c=0;
for(i=1;i<=num;i++)
{
if(num%i==0)
{
c=c+1;
}
}
if(c==2)
{
alert("no is prime");
}
else
{
alert("no is not prime");
}
}
</script>
</head>
<body>
<form="myform" method="post" >
enter number:<input type="text" name="no" />
<input type="button" onclick="prime()" value=result />
</form>
</body>
</html>


any number given as input ,its always shows "no is not a prime number"....i don't know where my logic is getting wrong....
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Question • Score: 0 • Views: 512 • Replies: 0
No top replies

 
 

Related Topics

 
  1. Forums
  2. » check whether the user entered number is prime or not using javascript
Copyright © 2019 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.03 seconds on 10/22/2019 at 10:42:19