JS Basic
JS Interview Programs
- JS Interview Programs
- JS Abundant Number
- JS Amicable Number
- JS Armstrong Number
- JS Average of N Numbers
- JS Automorphic Number
- JS Biggest of three numbers
- JS Binary to Decimal
- JS Common Divisors
- JS Composite Number
- JS Condense a Number
- JS Cube Number
- JS Decimal to Binary
- JS Decimal to Octal
- JS Disarium Number
- JS Even Number
- JS Evil Number
- JS Factorial of a Number
- JS Fibonacci Series
- JS GCD
- JS Happy Number
- JS Harshad Number
- JS LCM
- JS Leap Year
- JS Magic Number
- JS Matrix Addition
- JS Matrix Division
- JS Matrix Multiplication
- JS Matrix Subtraction
- JS Matrix Transpose
- JS Maximum Value of an Array
- JS Minimum Value of an Array
- JS Multiplication Table
- JS Natural Number
- JS Number Combination
- JS Odd Number
- JS Palindrome Number
- JS Pascalβs Triangle
- JS Perfect Number
- JS Perfect Square
- JS Power of 2
- JS Power of 3
- JS Pronic Number
- JS Prime Factor
- JS Prime Number
- JS Smith Number
- JS Strong Number
- JS Sum of Array
- JS Sum of Digits
- JS Swap Two Numbers
- JS Triangular Number
JavaScript Program to Check Prime Number
Photo Credit to CodeToFun
π Introduction
In the landscape of programming, dealing with prime numbers is a fundamental and interesting task. Prime numbers, those divisible only by 1 and themselves, play a crucial role in various mathematical and computational applications.
In this tutorial, we will explore a JavaScript program to check whether a given number is a prime number.
π Example
Let's take a look at the JavaScript code that performs the prime number check.
// Function to check if a number is prime
function isPrime(number) {
// 0 and 1 are not prime numbers
if (number <= 1) {
return false;
}
// Check for factors from 2 to the square root of the number
for (let i = 2; i * i <= number; ++i) {
if (number % i === 0) {
return false; // Found a factor, not a prime number
}
}
return true; // No factors found, it's a prime number
}
// Driver program
// Replace this value with your desired number
const testNumber = 17;
// Call the function to check if the number is prime
if (isPrime(testNumber)) {
console.log(`${testNumber} is a prime number.`);
} else {
console.log(`${testNumber} is not a prime number.`);
}
π» Testing the Program
To test the program with a different number, simply replace the value of testNumber in the code.
17 is a prime number.
Run the script to see whether the number is a prime number.
π§ How the Program Works
- The program defines a function isPrime that takes an integer as input and returns a boolean indicating whether the number is prime.
- Inside the function, it checks if the number is less than or equal to 1; if so, it's not prime.
- It then iterates through potential factors from 2 to the square root of the number.
- If it finds any factor, the number is not prime; otherwise, it is prime.
- The testNumber variable is used to test the program with a specific number (in this case, 17).
π Between the Given Range
Let's dive into the javascript code that checks and lists prime numbers in the specified range.
// Function to check if a number is prime
function isPrime(num) {
if (num <= 1) {
return false;
}
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return false;
}
}
return true;
}
// Function to list prime numbers in the range
function listPrimesInRange(start, end) {
let primeNumbers = [];
for (let i = start; i <= end; i++) {
if (isPrime(i)) {
primeNumbers.push(i);
}
}
return primeNumbers;
}
// Driver program
// Define the range
const startRange = 1;
const endRange = 20;
// Call the function to list prime numbers
const primeNumbersInRange = listPrimesInRange(startRange, endRange);
console.log(`Prime Numbers in the Range ${startRange} to ${endRange}:`);
console.log(primeNumbersInRange.join(' '));
π» Testing the Program
The program is set to check and list prime numbers in the range from 1 to 20. No additional input is required.
Prime Numbers in the Range 1 to 20: 2 3 5 7 11 13 17 19
Run the program to see the prime numbers in the specified range.
π§ How the Program Works
- The program defines two functions: isPrime to check if a number is prime, and listPrimesInRange to list prime numbers in a specified range.
- The isPrime function checks if a number has only two distinct positive divisors.
- The listPrimesInRange function iterates through the specified range, calls isPrime for each number, and compiles a list of prime numbers.
- The main section defines the range from 1 to 20 and calls the function to list prime numbers.
π§ Understanding the Concept of Prime Numbers
Before diving into the code, let's briefly understand prime numbers.
A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. For example, 2, 3, 5, 7, 11, and 13 are prime numbers.
π’ Optimizing the Program
While the provided program is effective, you can explore optimizations such as using the square root of the number as the loop limit for improved efficiency.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (JavaScript Program to Check Prime Number), please comment here. I will help you immediately.