JavaScript – While Loop

While loop: We use while loop when we don’t know the number of iterations of loop.

Program to count the number of digits in the given number:

<html>
            <head>
                        <script>
                                    function digitsCount(){
                                                var n = parseInt(document.getElementById(“num”).value);
                                                var count=0;
                                                while(n>0){
                                                            n = parseInt(n/10);
                                                            count++;
                                                }
                                                document.write(“Number of Digits : ” + count);
                                    }
                        </script>
            </head>
            <body>
                        Enter Number : <input type=”text” id=”num”/>
                        <button onclick=”digitsCount()”> Digits Count </button>
            </body>
</html>

Program to find the Sum of digits in the given number:

<html>
            <head>
                        <script>
                                    function digitsSum(){
                                                var n = parseInt(document.getElementById(“num”).value);
                                                var sum=0;
                                                while(n>0){
                                                            var rem = n%10;
                                                            sum = sum + rem;
                                                            n = parseInt(n/10);
                                                }
                                                document.write(“Sum of digits : ” + sum);
                                    }
                        </script>
            </head>
            <body>
                        Enter Number : <input type=”text” id=”num”/>
                        <button onclick=”digitsSum()”> Digits Sum </button>
            </body>
</html>

Reverse Number Program:

<html>
            <head>
                        <script>
                                    function reverse(){
                                                var n = parseInt(document.getElementById(“num”).value);
                                                var rev=0;
                                                while(n>0){
                                                            var rem = n%10;
                                                            rev = rev*10 + rem;
                                                            n = parseInt(n/10);
                                                }
                                                document.write(“Reverse number is : ” + rev);
                                    }
                        </script>
            </head>
            <body>
                        Enter Number : <input type=”text” id=”num”/>
                        <button onclick=”reverse()”> Reverse Number </button>
            </body>
</html>
Scroll to Top