Practical – 117.1 : Check Given Number is Prime or Not

Practical-117.1

Program Code : 

<!DOCTYPE html>

<html>

<head>

  <title>Prime Number Checker</title>

  <style>

    body {

      font-family: Arial;

      text-align: center;

      margin-top: 50px;

    }

    input, button {

      font-size: 18px;

      margin: 5px;

      padding: 8px;

    }

    #result {

      font-weight: bold;

      margin-top: 15px;

    }

  </style>

</head>

<body>

  <h2>Check Prime Number</h2>

  <input type=”number” id=”number” placeholder=”Enter a number”>

  <button onclick=”checkPrime()”>Check</button>

  <div id=”result”></div>

  <script>

    function checkPrime() {

      const num = parseInt(document.getElementById(“number”).value);

      let isPrime = true;

      if (num <= 1) {

        isPrime = false;

      } else {

        for (let i = 2; i <= Math.sqrt(num); i++) {

          if (num % i === 0) {

            isPrime = false;

            break;

          }

        }

      }

      const result = document.getElementById(“result”);

      if (isPrime) {

        result.textContent = num + ” is a Prime Number.”;

        result.style.color = “green”;

      } else {

        result.textContent = num + ” is Not a Prime Number.”;

        result.style.color = “red”;

      }

    }

  </script>

</body>

</html>