# Javascript to reverse a given number

By | May 5, 2017

This program uses a Javascript to reverse a given number. This is also known as the implementation of a palindrome. The program is quite simple. It asks you to enter a number and reverses the same for you. You can find the images for inputs and outputs here.

Input:

Pop-out box to enter an input number

```
<html>
<script type ="text/javascript">
function rev_num()
{
var num = prompt("Enter the number to be reveresed :", " ");
var n= num;
var rev = 0, rem;
while (n>0)
{
rem = n % 10;
rev = rev * 10 + rem ;
n = Math.floor(n/10);
}
document.write("The given number is : " +num+ " <br/> The reversed number is : " +rev+ "\n");
}
</script>
<body>
</html>

```

Output 1:

Output 2:

There is an alternative method to reverse a given number using split(), reverse() and join() functions available in javascript. The following code explains this.

```
<html>
<script type ="text/javascript">

function rev_num()
{
let num = prompt("Enter the number to be reveresed :", " ");
let str = num.toString().split('').reverse().join('');
let rev = parseInt(str);

document.write("The given number is : " +num+ " The reversed number is : " +rev+ "\n");
}

</script>
<body>
</html>

```

Input:

Diagram shows the pop-up box to input a number to be reversed

Output:

Diagram shows the output of a reversed number

Author: Ravi Bandakkanavar

A Techie, Blogger, Web Designer, Programmer by passion who aspires to learn new Technologies every day. It has been 6 years since I have been publishing articles and enjoying every bit of it. I want to share knowledge and build a great community with people like you.

## 3 thoughts on “Javascript to reverse a given number”

1. Detlef

Another way could be:

function rev_num() {

let num = prompt(“Enter the number to be reveresed :”, ” “);

let str = num.toString().split(”).reverse().join(”);

let rev = parseInt(str);

document.write(“The given number is : ” +num+ ” The reversed number is : ” +rev+ “\n”);
}