Script Categories













Calculators >>> Prime Number.

Enter a number and JavaScript can instantly tell you if it is a prime number or not.

Prime Number Calculator

Please enter a number:


Add the below code to the <body> section of your page:

<script language="javascript" type="text/javascript">
/* Visit http://www.yaldex.com/ for full source code
and get more free JavaScript, CSS and DHTML scripts! */
<!-- Begin
function calculate(form) {
var num=parseInt(form.number.value);
if (isNaN(num) || num < 0) {
form.result.value=(form.number.value + " is not a valid number!  Try again!");
}
if (num == 1 || num == 2) {
form.result.value=(num + " is prime!");
}
for (var i=2;i<num;i++) {
if (num % i == 0) {
var prime="yes";
form.result.value=(num + " is not prime.  It is divisible by " + i + ".");
break;
}
if (num % i != 0) var prime="no";
}
if (prime == "no") form.result.value=(num + " is prime!");
}

// End -->
</SCRIPT>
<form name=form>
<h2>Prime Number Calculator</h2>
Please enter a number:<br>
<input type=text name=number size=7>
<input
type="button" value="Calculate" onClick="calculate(this.form)">
<br><br>
<input type=text name=result size=45 value="">
</form>

JavaScript Editor Get Advanced
JavaScript and Ajax Editor,
Validator and Debugger!

1st JavaScript Editor.



Code was highlighted by 1st JavaScript Editor (The Best JavaScript Editor!).

R7


©