PROGRAMS

Javascript to reverse a given number

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 refer to the images for inputs and outputs.

 

We have explained two different methods to reverse a number here.

//Palindrome.html
<!DOCTYPE HTML>
<html>
<title>Palindrome.html</title>
<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 style="background-color:green" onload="rev_num();"> <!-- when the page gets loaded rev_num() function gets called -->
<body>
</html>

Suggested Read:

 

Input:

Pop-out box to enter an input number

Output 1:

Output 2:

The following video explains how to execute the javascript:

Alternative Method

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

//Palindrome2.html
<!DOCTYPE HTML>
<html>
<title>Palindrome2.html</title>
<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 style="background-color:green" onload="rev_num();"> <!-- when the page gets loaded rev_num() function gets called -->
<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

 

How to execute this script?

  1. Copy the code and save it in a text file with a .html extension.
  2. Open the file saved with a .html extension in a browser.
  3. You will be asked to enter a number/series and the result gets displayed on the same HTML page.
  4. To check for another number/series, refresh the page and again enter a new number. You can also refer to the video for easy understanding.

Please comment if you face any difficulties.

Ravi Bandakkanavar

A Techie, Blogger, Web Designer, Programmer by passion who aspires to learn new Technologies every day. A founder of Krazytech. It's been 10+ years since I am publishing articles and enjoying every bit of it. I want to share the knowledge and build a great community with people like you.

Leave a Comment

View Comments

  • 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");
    }

Share
Published by
Ravi Bandakkanavar

Recent Posts

Everything You Need To Know About Create React App

The configuration of resources can be a time-consuming and difficult operation while creating a React…

3 weeks ago

Causes of CyberCrime and Preventive Measures

          Rapid technological growth and developments have provided vast areas of…

1 month ago

How Data Lineage will Improve Business Practices

How often have you thought about changing the way that you store and use data?…

1 month ago

10 Dominating Programming Language for Mobile App In 2022

Programming Languages are a set of rules that aid in transforming a concept into a…

1 month ago

What is Serverless Edge Computing? Features | Benefits

Serverless edge computing is a new technology with a lot of promise, but it can…

1 month ago

Are Your Accounts Protected Against Cyberattacks?

Do any of your passwords include personal names, date of birth, or pet names? If…

2 months ago