Tuesday, February 27, 2018

Prime Number Checker in JQuery

In this article I would like to share with you a sample program to accept a number from our user and then our program will check if the given number in a Prime Number or Not a Prime Number using JQuery.

I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental is (034) 4335675.




Sample Program Output


Program Listing

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js">
</script>
  <meta charset="utf-8">
  <style>
    body {
         background-color: lightgreen;
         font-family: arial;
         font-size: 15px;

    }
  </style>
  <script>
    $(document).ready(function() {
       var res = 0;
         
         var number = parseInt($("#txtnum").val());
         $("#txtnum").keypress(function(e) {

           if (e.which >= 48 && e.which <= 57) {
                
           }
           else {
             $("#error").html("Please enter only number number").show().fadeOut("slow");
             return false;
           }
         });
         $("#check").click(function() {
             res = 0;
             var number = parseInt($("#txtnum").val());
         
           for (var i = 1; i < number; i++) {
             if (number % i == 0) {
               res++;
             }
           };
             console.log(res);
           if (res >= 2) {
             $("#error").html("The given number is not a prime number.").show().fadeOut(5000);
             return false;
           }
           else {
             $("#error").html("The given number is a prime number.").show().fadeOut(5000);
           };
         });
       });
  </script>
</head>
<body>
    <table align="left">
    
    <tr>
      <td>
      </td>
      <td align="left">
          <h2>PRIME NUMBER CHECKER IN JQUERY</h2>
      </td>
      <td>
      </td>
    </tr>
    <tr>
      <td>
       Give a Number
      </td>
      <td>
        <input type="text" id="txtnum">
      </td>
      <td>
        <span id="error"> </span>
      </td>
    </tr>
    <tr>
      <td>
      </td>
      <td align="left">
         <input type="submit" value="Check Number" id="check">
      </td>
      <td>
      </td>
    </tr>
  </table>
</body>
</html>



No comments:

Post a Comment