Skip to main content

How To Reverse A Number Using JavaScript


In this tutorial, we are going to learn on How To Reverse A Number Using JavaScript. This program runs that the user will type the desired number then the program will execute the script to reverse the given number from the user. Let's start with:

JavaScript

This script will use to reverse the given number.

  1. <script>
  2. var reverse_number =0;
  3.  
  4. var num_value = parseInt(prompt("Type your number to Reverse: "));
  5. document.write("<br>");
  6. document.write("<h3>How To Reverse A Number Using JavaScript</h3>");
  7. document.write("<b style='color:blue;'>The Original Number is <b style='color:red;'>" + num_value + "</b> .</b><br />");
  8. while( num_value != 0 )
  9. {
  10. reverse_number = parseInt(reverse_number * 10);
  11. reverse_number = parseInt(reverse_number + (num_value%10));
  12. num_value = parseInt(num_value/10);
  13. }
  14. document.write("<b style='color:blue;'>The Reverse Number is <b style='color:red;'>" + reverse_number + "</b> .</b><br>");
  15. </script>

The Complete Code

Copy and paste to your editor to try this simple tutorial. Enjoy coding.

  1. <!DOCTYPE html>
  2.  
  3.  
  4. <title>How To Reverse A Number Using JavaScript</title>
  5.  
  6. <style type="text/css">
  7. body {
  8. width:500px;
  9. margin:auto;
  10. text-align:center;
  11. }
  12. h3 {
  13. color:blue;
  14. }
  15. </head>
  16.  
  17. <body>
  18. var reverse_number =0;
  19.  
  20. var num_value = parseInt(prompt("Type your number to Reverse: "));
  21. document.write("<br>");
  22. document.write("<h3>How To Reverse A Number Using JavaScript</h3>");
  23. document.write("<b style='color:blue;'>The Original Number is <b style='color:red;'>" + num_value + "</b> .</b><br />");
  24. while( num_value != 0 )
  25. {
  26. reverse_number = parseInt(reverse_number * 10);
  27. reverse_number = parseInt(reverse_number + (num_value%10));
  28. num_value = parseInt(num_value/10);
  29. }
  30. document.write("<b style='color:blue;'>The Reverse Number is <b style='color:red;'>" + reverse_number + "</b> .</b><br>");
  31. </body>
  32. </html>

Hope that this tutorial will help you a lot.

So what can you say about this work? Share your thoughts in the comment section below or email me at [email protected]. Practice Coding. Thank you very much.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.