How To Reverse A Number Using JavaScript

Submitted by: 
Language: 
Visitors have accessed this post 800 times.

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.


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.