Palindrome Number

palindrome number
PHP

Palindrome Number

Write a program to find the number is Palindrome number or not

A number is called Palindrome when its reverse is same as the orginal number.

Eg:- Number = 12321, Reverse = 12321 Both the number and reverse is same thus its called as a palindrome number.

Program :

<?php
$number = 12321;  
$num = 12321; 
$sum = 0;  
while(floor($number)) 
{  
    $rem = $number % 10;  
    $sum = $sum * 10 + $rem;  
    $number = $number/10;  
}  

if($num==$sum)
{  
    echo "$num is a Palindrome number";  
} 
else 
{  
    echo "$num is not a Palindrome";  
}  
?>
  1. Define a number which you want to find the reverse to $number
  2. Save the number to another variable $num, to equate with the reverse
  3. Declare a variable $sum=0
  4. Loop the $number 
  5. Get the reminder of $number  by $number %10
  6. Multiply the sum with 10 and add the reminder
  7. Divide the $number  by 10
  8. Equate the $sum with $num as it contains the reverse number
  9. If both are equal the number is palindrome, else its not a palindrome number.

Logic :

$number = 12321

1st loop
$sum = 0
while(floor(12321))
{
$rem = $num%10 = 12321 % 10 = 1
$sum = ($sum * 10)+$rem = (0 * 10) + 1 = 0+1 = 1
$num = $num/10 = 12321/10= 1232
}

2nd loop
$sum = 1
while(floor(1232))
{
$rem = $num%10 = 1232 % 10 = 2
$sum = ($sum * 10)+$rem = (1 * 10) + 2 = 10+2 = 12
$num = $num/10 = 1232/10= 123
}

3rd loop
$sum = 12
while(floor(123))
{
$rem = $num%10 = 123 % 10 = 3
$sum = ($sum * 10)+$rem = (12 * 10) + 3 = 120+3 = 123
$num = $num/10 = 123/10= 12
}

4th loop
$sum = 123
while(floor(12))
{
$rem = $num%10 = 12 % 10 = 2
$sum = ($sum * 10)+$rem = (123 * 10) + 2 = 1230+2 = 1232
$num = $num/10 = 12/10= 1
}

5th loop
$sum = 1232
while(floor(1))
{
$rem = $num%10 = 1 % 10 = 1
$sum = ($sum * 10)+$rem = (1232 * 10) + 1 = 12320+1 = 12321
$num = $num/10 = 1/10= .1
}

Loop end as the number became 0.1

Click here for the program factorial in php

Click here for more knowledge on Php

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top