Prime Numbers
A number greater than 1 with exactly two factors, i.e. 1 and the number itself is defined as a prime number. In other words, if a number cannot be divided into equal groups, then it is a prime number. We can divide a number into groups with equal numbers of items/elements only if it can be factorized as a product of two numbers. For example, 7 cannot be divided into groups of equal numbers. This is because 7 can only be factorized as follows:
7 x 1, 1 x 7
program for prime numbers in javascript
It is recommended to use dark mode to see the code in the better way. Double-click to select the code👇
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Prime/Not Prime</title>
<style>
form{
margin-left: 23%;
margin-right: 23%;
height: auto;
width: 400px;
background-color: mediumslateblue;
text-align: center;
}
input{
height: 40px;
width: 300px;
}
h1{
text-align: center;
}
</style>
<script type="text/javascript">
function prime() {
var n = parseInt(document.getElementById("a").value);
var i, count = 0;
for (i = 2; i < n; i++) {
if (n % i == 0) {
count = 1;
break;
}
}
if (count == 1) {
var result = "" + n + " is not a prime number";
} else {
var result = "" + n + " is a prime number";
}
var v = document.getElementById("primeno");
v.value = result;
}
</script>
</head>
<body>
<h1>Find prime number using javascript</h1>
<form>
<fieldset>
First Number: <input type="number" id="a" maxlength="14" oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);prime()"><br><br>
Result: <br><input type="text" id="primeno" disabled><br><br>
</fieldset>
</form>
</body>
</html>
We Love Hearing from You!
Thank you for reading our post! Your thoughts and opinions are important to us. Please leave a comment below to share your feedback, ask questions, or start a discussion. We look forward to engaging with you!
Note: Comments are moderated to ensure a respectful and positive environment.