JavaScript – For Loop Programs

Display the multiplication table of given input number:

<html>
            <head>
                        <script>
                                    function printTable(){
                                                var n = parseInt(document.getElementById(“num”).value);
                                                for(var i=1 ; i<=10 ; i++){
                                                            document.write(n + “x” + i + “=” + (n*i) + “<br/>”);
                                                }
                                    }
                        </script>
            </head>
            <body>
                        Enter table number : <input type=”text” id=”num”/>
                        <button onclick=”printTable()”> print table </button>
            </body>
</html>

Print only even numbers in the given range:

<html>
            <head>
                        <script>
                                    function evens(){
                                                var n = parseInt(document.getElementById(“range”).value);
                                                for(var i=1 ; i<=n ; i++){
                                                            if(i%2==0)
                                                                        document.write(i + “<br/>”);
                                                }
                                    }
                        </script>
            </head>
            <body>
                        Enter Limit : <input type=”text” id=”range”/>
                        <button onclick=”evens()”> Even Numbers </button>
            </body>
</html>

Display Factors for the given number:

<html>
            <head>
                        <script>
                                    function factors(){
                                                var n = parseInt(document.getElementById(“num”).value);
                                                for(var i=1 ; i<=n ; i++){
                                                            if(n%i==0)
                                                                        document.write(i + ” is a factor” + “<br/>”);
                                                }
                                    }
                        </script>
            </head>
            <body>
                        Enter Number : <input type=”text” id=”num”/>
                        <button onclick=”factors()”> Find factors </button>
            </body>
</html>

Program Count the number of factors for the given number:

<!doctype html>
<html>
            <head>
                        <script>
                                    function factors(){
                                                var n = parseInt(document.getElementById(“num”).value);
                                                var count=0;
                                                for(var i=1 ; i<=n ; i++){
                                                            if(n%i==0)
                                                                        count++;
                                                }
                                                document.write(“Factors count is : ” + count);
                                    }
                        </script>
            </head>
            <body>
                        Enter Number : <input type=”text” id=”num”/>
                        <button onclick=”factors()”> Count Factors </button>
            </body>
</html>

Prime number: The number which is having 2 factors

<html>
            <head>
                        <script>
                                    function prime(){
                                                var n = parseInt(document.getElementById(“num”).value);
                                                var count=0;
                                                for(var i=1 ; i<=n ; i++){
                                                            if(n%i==0)
                                                                        count++;
                                                }
                                                if(count==2)
                                                            document.write(n + ” is prime number”);
                                                else
                                                            document.write(n + ” is not prime number”);
                                    }
                        </script>
            </head>
            <body>
                        Enter Number : <input type=”text” id=”num”/>
                        <button onclick=”prime()”> Check Prime or Not </button>
            </body>
</html>
Scroll to Top