a number is a palindrome if the reversed number is equal to the original number. for example, 121, 101, and 12321 are palindrome numbers.
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>
Input has been taken through the javascript prompt method
enter a number:12321 12321 is palindrome a number
n
.copy
to check it when we need to compare our original number with the reverse number.n%10
and rev*10
will maintain their nth position.if(copy==rev) { }
.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
enter a number:23432 23432 is palindrome a number
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
enter a number:53435 53435 is palindrome a number