Table of Contents
show
Example 32
Write a for loop to find prime numbers between 1 to 50
function isPrime(num) {
if (num <= 1) {
return false;
}
for (var i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) return false;
}
return true;
}
for (var i = 1; i <= 50; i++) {
if (isPrime(i)) {
console.log(i);
}
}
Output
Views: 67