Palindrome number in JavaScript

a number is a palindrome if the reversed number is equal to the original number. for example, 121, 101, and 12321 are palindrome numbers.

Palindrome in JavaScript using while loop

In this program, you will take input from users using a window prompt, and you will check whether the number is a palindrome or not using a while loop.

<!DOCTYPE html>
<html>
  <body>
    <script>
      function Palindrome() {
       var r,rev=0,n,copy;
        n = parseInt(prompt("enter a number:"));
	copy=n;
        while (n != 0) {
          r = n % 10;
          rev = rev * 10 + r;
          n = parseInt(n / 10);
        }
	if(copy==rev)
	{
           document.write(copy+" is palindrome a number");
	}
	else
	{
	   document.write(copy+" is not palindrome a number");
	}
      }
    </script>
    <form>
      <input type="button" value="Palindrome" onclick="Palindrome();" />
    </form>
  </body>
</html>
 
Run now

Input has been taken through the javascript prompt method

output

enter a number:12321
12321 is palindrome a number

explanation of palindrome program to check whether a number is palindrome or not:

  • when the user clicks on the palindrome button, Palindrome function will get called.
  • inside palindrome function asked the user to enter a positive number which is stored in the variable n.
  • The number is saved into another variable copy to check it when we need to compare our original number with the reverse number.
  • Inside the loop, one by one last digit number is separated using code n%10 and rev*10 will maintain their nth position.
  • After reversing the digits, compare a reverse value with the original value which is stored in the copy variable. you will compare an original value with the copy value by using the if statement i.e if(copy==rev) { }.
  • If the numbers are equal, then the number is a palindrome, otherwise, it's not.

Program to check Palindrome number in JavaScript using for loop

In this program, you will take input by using a window prompt method, and you will check whether the number is a palindrome or not using a for loop.

<!DOCTYPE html>
<html>
  <body>
    <script>
      function Palindrome() {
         var r,rev=0,n,copy;
         n = parseInt(prompt("enter a number:"));
	 copy=n;
	for(;n != 0; n = parseInt(n / 10);)
        { 
	   //find reminder
           r = n % 10;
	   // reverse the number
           rev = rev * 10 + r;
         }
	 // compare reverse number with original number
	 if(copy==rev)
	 {
            document.write(copy+" is palindrome a number");
	  }
	 else
	 {
	    document.write(copy+" is not palindrome a number");
	 }
      }
    </script>
    <form>
      <input type="button" value="Palindrome" onclick="Palindrome();" />
    </form>
  </body>
</html>
                        
 

Input has been taken through the javascript prompt method

output

enter a number:23432
23432 is palindrome a number

Program to check Palindrome number in JavaScript using do while loop

In this program, you will check whether the number is a palindrome or not by using a do while loop.

<!DOCTYPE html>
<html>
  <body>
    <script>
      // check palindrome number by using do while loop
      function Palindrome() {
       var r,rev=0,n,copy;
	// take input from user stored in n
        n = parseInt(prompt("enter a number:"));
	copy=n;
        do{
	        // get reminder
                r = n % 10;
		// reverse the number
                rev = rev * 10 + r;
                n = parseInt(n / 10);
        } while (n != 0);
	if(copy==rev)
	{
            document.write(copy+" is palindrome a number");
	 }
	 else
	 {
	     document.write(copy+" is not palindrome a number");
	 }
      }
    </script>
    <form>
      <input type="button" value="Palindrome" title="check palindrome number" onclick="Palindrome();" />
    </form>
  </body>
</html>                                 
 

Input has been taken through the javascript prompt method

output

enter a number:53435
53435 is palindrome a number