JavaScript

How to reverse a number in JavaScript

In this tutorial, we are going to see how to write a program to reverse the digits of a number in the JavaScript programming language.
 
Example :

Input: 123
Output: 321

 

Program to reverse a number in JavaScript
<!doctype html>
<html>
   <head>
      <script>
         function reverse()
         {
               var tmp=0, x, nbr, y;
			 
               nbr = Number(document.getElementById("Myinput").value);
			 
               y = nbr;
               while(nbr > 0)
               {
                     x = nbr%10;
                     nbr = parseInt(nbr/10);
                     tmp = tmp*10+x;
               }
               alert(tmp);
         }
      </script>
   </head>
   <body>
      Enter a number : <input id="Myinput">
      <button onclick="reverse()">Reverse</button>
   </body>
</html>
Result
Enter a number :
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *