JavaScript Prime Number Program
In this post, you will learn how to write a prime number program using JavaScript.
A prime number is a whole number greater than 1, whose only factors are 1 and itself, like -2, 3, 5, 7, 11, etc. For example, 17 is a prime number because it is only divisible by 1 and 17. On the other hand, 18 is not a prime number because it is divisible by 2, 3, 6, 9, and the number itself.
Print prime numbers between 1 to 100 using for loop JavaScript
Here, we have used a for loop to get prime numbers between 1 and 100. We loop over the number range (1 to 100) and check whether the number is prime or not. The for loop iterates from i = 2 to i = given number. If the remainder of n % i = 0, then it increases the count by 1.
// JavaScript program
// Finding prime numbers in the given range
// function to check if a given number is prime
function chkPrime(n)
{
if(n == 1 || n == 0) return false;
// Run a loop from 2 to n-1
for(var i = 2; i < n; i++)
{
//checking for number of divisor
if(n % i == 0) return false;
}
// otherwise, n is prime number.
return true;
}
// Driver code
// Prime Numbers between 1 and 100
var num = 100;
// check for every number from 1 to num
for(var i = 1; i <= num; i++)
{
// check if current number is prime
if(chkPrime(i)) {
console.log( i );
}
}
Output of the above code:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
Print prime Numbers between 1 to 100 using user input JavaScript
In the given webpage, we ask the user to provide a start and end number range. We loop over the number range using JavaScript and check whether the number is prime or not. The for loop iterates from i = 0 to i = given number. If the remainder of i/num = 0, then it increases the count by 1. After all the iterations, if counter=2, then that number is a prime number.
<html>
<head>
<title>JavaScript to print prime numbers</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
function printPrime() {
var i, j, counter;
// take input from the user
const min = parseInt(prompt('Enter lower number: '));
const max = parseInt(prompt('Enter higher number: '));
//loop till i equals to max
for (i = min; i <= max; i++) {
counter = 0;
for (j = 1; j <= i; j++) {
//checking for number of divisor
if (i % j == 0) {
//increment the value of counter
counter++;
}
}
// if the value of counter is 2
// then it is a prime number
if (counter == 2) {
document.getElementById("result").insertAdjacentHTML('beforeend', i + '<br>');
}
}
}
</script>
</head>
<body>
<h2>Prime numbers between given range: </h2>
<input type="submit" value="Print Prime Numbers" onclick="printPrime()" name="print" />
<div id="result"></div>
</body>
</html>
Output of the above code: Related Articles
Generate random numbers in JavaScriptJavascript window location
Remove duplicates from array Javascript
Scientific calculator JavaScript
How to reverse string in Javascript
JavaScript display PDF in the browser using Ajax call
Parsing JSON in Javascript
Javascript speech recognition example
Select/deselect all checkboxes using Javascript
Print specific part of a web page in javascript
Dynamically Add/Delete HTML Table Rows Using Javascript
jQuery Ajax serialize form data example
Image popup on page load using HTML and jQuery
Ajax live data search using jQuery PHP MySQL
jQuery loop over JSON result after AJAX Success
Simple star rating system using PHP, jQuery and Ajax
jquery image zoom on mouseover example
Bootstrap star rating input example
Bootstrap datepicker example
Submit a form data without page refresh