30. JavaScript Program to find the Reverse of a given number.
How does this program work?
- This program is used to find the Reverse of a given number using JavaScript.
- The integer entered by the user is stored in variable n.
- Declare variable r to store reverse number and initialize it with 0.
- Multiply the reverse number by 10, add the remainder which comes after dividing the number by 10.
Here is the code
<html>
<head>
<title>JavaScript program to find the Reverse of a given number</title>
</head>
<body>
<table>
<tr>
<td> <input type="text" name="a" id="first" placeholder="Enter a number"> </td>
</tr>
<tr>
<td> <button onclick="reverse ()" >Submit</button> </td>
</tr>
</table>
<div id = "num"></div>
</body>
<script type="text/javascript">
function reverse()
{
var a,n,r = 0;
n = parseInt(document.getElementById ("first").value);
while(n > 0)
{
a = n%10;
n = parseInt(n/10);
r = r*10+a;
}
document.getElementById ("num").innerHTML ="Reverse of a given number is : "+r;
}
</script>
</html>