Write a program to check the given number is prime or not.

1
2
    <html>
3
    <head>
4
        <script>
5
            function prime() {
6
                var n = parseInt(document.getElementById("num").value);
7
                var flag = 0;
8
9
                // Numbers less than 2 are not prime
10
                if (n <= 1) {
11
                    document.getElementById("out").innerHTML = n + " is not a prime number.";
12
                    return;
13
                }
14
15
                // Check divisibility from 2 to n / 2
16
                for (var i = 2; i <= n / 2; i++) {
17
                    if (n % i === 0) {
18
                        flag = 1;
19
                        break;
20
                    }
21
                }
22
23
                // Display the result
24
                if (flag === 0) {
25
                    document.getElementById("out").innerHTML = n + " is a prime number.";
26
                } else {
27
                    document.getElementById("out").innerHTML = n + " is not a prime number.";
28
                }
29
            }
30
        </script>
31
    </head>
32
33
    <body>
34
        <h2>Prime Number Checker</h2>
35
        Enter the number: <input type="number" id="num">
36
        <button type="button" onclick="prime()">Check</button>
37
        <hr>
38
        <p id="out"></p>
39
    </body>
40
    </html>
41
42
    

Output

1
2
        No Output Available
3
        

Rate the Code

Reviews are published after a quick safety check (usually within 24 hours).

Reviews are public. Developers can see basic device specifications to respond. Learn more

Core concept: Whether the user-input number is prime or not

This program calculates whether the user-provided number is a prime number or not.

In this program:

  • parseInt(document.getElementById(“num”).value; the value is extracted.
  • First, check if (n<=1) because 1 and numbers less than 1 are not prime numbers.
  • Then, the for loop is used to check whether the number is divisible by 2 and onward up to n/2.
  • If any divisor is found, then the user's number (n) is not prime; else, it is prime.
  • Flag = 1 if any prime number is found; then break the for loop.

Why is it used?

  • To explain the basic mathematical algorithms and programming logic.
  • To implement basic cryptography and number theory concepts on a web interface.

Real-Life Applications

  • Data Security & Cryptography: Prime numbers play an important role in RSA encryption and secure data transmission algorithms.
  • Math & Educational tool: To build online quick validation tools for science or math calculators.
License: This program's source code is open-sourced under the MIT License, while the rest of the program's text content and overall dhimanp platform and brand content remain proprietary under dhimanp.

Submit your modified code

Your submissions are published after a quick safety check (usually within 7 days).

Submissions are public. Developers can see basic device specifications to respond. Learn more

Disclaimer

The information that you shared with us will be publicly visible, and everyone can see your information. And we (dhimanp) are not responsible if anyone misuses this information. We can remove any submitted code at any time for better transparency.

Privacy Policy

If you share your data with us by submitting your code for credit, then we may show your full name, school or college name, and city name on our website.

We are not using this data to track you and are not selling this data to anyone.

If you want to remove your name from our website, you can contact us, and we will remove your name from our website. In the removal process, full credit, including your full name, school or college, city, and code, will be removed from our website.