JavaScript program to reverse the number entered by the user, then displays the reversed number on the screen.
For example, if the user enters 678 as input, 876 is displayed as output. We use the mod(%) operator in the program to get the digits of an integer.
JavaScript Program Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<script> var n, reverse = 0; n=Number(window.prompt("Enter an integer",0)); while(n != 0) { reverse = reverse * 10; reverse = reverse + n%10; n = Math.floor(n/10); } window.alert("Reverse of the number is " + reverse); </script> |
Alternative:
1 2 3 4 5 6 7 8 9 10 11 |
<script> var n, reverse = 0; n=Number(window.prompt("Enter an integer",0)); var reverse = Number(String(Math.abs(n)).split('').reverse().join('')); window.alert("Reverse of the number is " + reverse); </script> |