Write a JavaScript to Program to Check Whether a Number is Prime or Not – through user input | How to check if a number is prime in JavaScript

161 views

Handle Base Cases: The function first checks if the number is less than or equal to 1. Prime numbers are defined as natural numbers greater than 1, so these cases immediately return false.

Iterate and Check Divisibility: A for loop iterates from i = 2 up to the square root of the number.

  • The loop starts from 2 because 1 is not a prime factor to check, and any number is divisible by 1.
  • The loop goes up to Math.sqrt(number) for optimization. If a number n has a divisor d > sqrt(n), then it must also have a divisor n/d < sqrt(n). Therefore, checking up to the square root is sufficient.

Return false if divisible: Inside the loop, number % i === 0 checks if number is perfectly divisible by i. If it is, number has a factor other than 1 and itself, so it is not prime, and the function returns false.

πŸ‘‰ Download Free Ebook β†’

πŸ‘‰ Get Complementary β†’

right side banner -- www.w3webmart.com

Return true if no divisors found: If the loop completes without finding any divisors, it means the number is only divisible by 1 and itself, making it a prime number. The function then returns true.

Find Live Demo

πŸ’» Example: HTML and JavaScript

  1.  
  2.   <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>Prime NUMBER Checker</title>
  8.     <style>
  9.         body {
  10.             font-family: Arial, sans-serif;
  11.             display: flex;
  12.             justify-content: center;
  13.             align-items: center;
  14.             min-height: 100vh;
  15.             background-color: #f4f4f4;
  16.             margin: 0;
  17.         }
  18.         .container {
  19.             background-color: #fff;
  20.             padding: 30px;
  21.             border-radius: 8px;
  22.             box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  23.             text-align: center;
  24.         }
  25.         INPUT[TYPE="number"] {
  26.             padding: 10px;
  27.             margin-bottom: 15px;
  28.             border: 1px solid #ddd;
  29.             border-radius: 4px;
  30.             width: 200px;
  31.             font-SIZE: 16px;
  32.         }
  33.         button {
  34.             padding: 10px 20px;
  35.             background-color: #007bff;
  36.             color: white;
  37.             border: NONE;
  38.             border-radius: 4px;
  39.             cursor: pointer;
  40.             font-SIZE: 16px;
  41.         }
  42.         button:hover {
  43.             background-color: #0056b3;
  44.         }
  45.         #result {
  46.             margin-top: 20px;
  47.             font-SIZE: 18px;
  48.             font-weight: bold;
  49.         }
  50.     </style>
  51. </head>
  52. <body>
  53.     <div class="container">
  54.         <h1>Prime NUMBER Checker</h1>
  55.         <INPUT TYPE="number" id="numberInput" placeholder="Enter a number">
  56.         <button onclick="checkPrimeNumber()">Check</button>
  57.         <p id="result"></p>
  58.     </div>
  59.  
  60.     <script>
  61.         FUNCTION isPrime(num) {
  62.             IF (num <= 1) {
  63.                 RETURN FALSE; // Numbers less than OR equal TO 1 are NOT prime
  64.             }
  65.             FOR (let i = 2; i <= Math.sqrt(num); i++) {
  66.                 IF (num % i === 0) {
  67.                     RETURN FALSE; // IF num IS divisible BY any NUMBER other than 1 AND itself
  68.                 }
  69.             }
  70.             RETURN TRUE; // IF no divisors were found, num IS a prime NUMBER
  71.         }
  72.  
  73.         FUNCTION checkPrimeNumber() {
  74.             const numberInput = document.getElementById('numberInput');
  75.             const resultParagraph = document.getElementById('result');
  76.             const num = parseInt(numberInput.value);
  77.  
  78.             IF (isNaN(num)) {
  79.                 resultParagraph.textContent = "Please enter a valid number.";
  80.                 resultParagraph.style.color = "red";
  81.                 RETURN;
  82.             }
  83.  
  84.             IF (isPrime(num)) {
  85.                 resultParagraph.textContent = `${num} is a prime number.`;
  86.                 resultParagraph.style.color = "green";
  87.             } ELSE {
  88.                 resultParagraph.textContent = `${num} is not a prime number.`;
  89.                 resultParagraph.style.color = "red";
  90.             }
  91.         }
  92.     </script>
  93. </body>
  94. </html>

 

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

How to check if an input is a prime number?

A prime number is not divisible by any other number except itself and 1. Hence, one possible way to check if n is prime is to ensure that it is not divisible by any number from 2 up to n / 2 (if n / 2 is a decimal number, then round it down to the nearest integer).

How to check whether a number is prime or not?

To check if a number is prime, determine if it is a natural number greater than 1 and if its only divisors are 1 and itself. A common method is trial division: check divisibility by integers from 2 up to the square root of the number; if it's divisible by any of these, it's not prime. If it's not divisible by any number in that range, it is prime.

Is there a trick to find prime numbers?

To determine if a number is prime, use trial division by dividing it by prime numbers starting from 2 up to its square root. First, quickly check for obvious factors: if it's an even number (except 2), it's not prime; if the sum of its digits is divisible by 3, it's not prime. If the number survives these quick checks, then proceed to test divisibility by other primes. If it's not divisible by any prime number up to its square root, then the number is prime.

πŸ‘‰ Get Complementary β†’

right side banner -- www.w3webmart.com
 
  
πŸ‘‰ Get Free Course β†’

πŸ“Œ Salesforce Administrators

πŸ“Œ Salesforce Lightning Flow Builder

πŸ“Œ Salesforce Record Trigger Flow Builder

πŸ‘‰ Get Free Course β†’

πŸ“Œ Aura Lightning Framework

πŸ“Œ Lightning Web Component (LWC)

πŸ“Œ Rest APIs Integration



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net