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

In this javascript tutorial we are learning about whether a check number is prime or not using javascript

Prime number

A prime number is an integer number which is divide by 1 and itself only 

Prime number is greater than 1. Prime number is not less than 1.

Prime number can't divide by its remainder.

Ex. 5 is a prime number because 5 is not divided by its remainder by 2, 3, 4.


example

Input : 4

Output : not prime number


input : 5

Output : is a prime number


Logic

for(i=2;i<=n-1;i++)

                if(n%i==0)

                    {

                        flag=false;

                        break;

                    }

            if(flag==true)

                alert(n+"is a prime number");

            else

                alert(n+"is no a prime number")

        }


//write a javascript program to check the given number is prime or not


<html>

<head>

    <title> prime number </title>

    <script type="text/javascript">

        //function to check prime number

        function p(){

            var n, i, flag=true;

            // getting the value from the from text

            //field using DOM 

            n=document.myform.n.value;

            n=parseInt(n)

            for(i=2;i<=n-1;i++)

                if(n%i==0)

                    {

                        flag=false;

                        break;

                    }

            //check and display aleart message

            if(flag==true)

                alert(n+"is a prime number");

            else

                alert(n+"is no a prime number")

        }

    </script>

    </head>

    <body style="font-size:30px">

    <h2>javascript to find prime number or not</h2>

        <form name="myform">

        enter a number:<input type="text" name=n value=" ">

            <br><br>

            <input type="button" value="click" onclick="p()">

            <br>

        </form>

    </body>

</html>


output:
write a javascript program to check the given number is prime or not.













Post a Comment

0 Comments