PHP Program to check the number is palindrome or not using function


April 22, 2022, Learn eTutorial
1515

What is a palindrome number?

Palindrome numbers are those numbers whose reverse is exactly the same as the actual number. For example, if we take 5665 and the input and by checking the reverse of the number it will also be 5665 itself such numbers are called the palindrome numbers. 

What is a palindrome

How to check the number is a palindrome or not using PHP?

In this program, we are using the user-defined function to find the reverse of the number and comparing it with the entered number to check that the inputted number is palindrome or not. First of all, we need to accept the input from the user and store the inputted number into a variable then we have to call the function created to check the reverse of the number or not and pass the value of the variable number as an argument of the function. Then we have to find the reverse of the number by first find the Mod(%) of the number and assign it to the variable rem then we have to multiply by 10 with the variable rev and add the current value in the variable rem to it and assign it to the variable rev and after that divide the number by 10 to avoid the last digit of the number and assign the number into the variable number iterate these operations using while loop until every digit of the variable is reversed. And after the completion of the iteration, we have to return the value of the variable rev as the result of the function findReverse(). After that er have to compare the result with the entered number if both are the same then the entered number is a palindrome number otherwise it is not a palindrome number.

ALGORITHM

Step 1: Accept the number to check from the user and assign it to the variable number

Step 2: Assign the return value of the user-defined function findReverse() with variable number as the argument into variable checkNum

Step 3: By using the if statement checks the condition value of the variable number is equal to the value of the variable checkNum if the condition is true print the value of a variable number as Palindrome number otherwise print the value of the variable number is not a palindrome number

 

Algorithm User-Defined Function: findReverse(n)

Step 1: Assign the argument passed from the function to the variable num

Step 2: Assign the value 0 into the variable rev to store the reverse of the number

Step 3: Perform the following sub-steps until the condition 'num > 1'

        (i) Assign the computed result of 'num % 10' into the variable rem to store the remainder

        (ii) Assign the computed result of 'rev * 10 + rem' into the variable rev to store the reverse of the number

        (iii) Assign the computed result of 'num / 10' into the variable num to store the number by avoiding the last digit of the number

Step 4: After the completion of the above step we have to return the value of the variable rev

PHP Source Code

                                          <?php
function findReverse($n)
{
    $num = $n;
    $rev = 0;
    while ($num > 1) {
        $rem = $num % 10;
        $rev = $rev * 10 + $rem;
        $num = $num / 10;
    }
    return $rev;
}
$number = readline("Enter the number: ");
$checkNum = findReverse($number);
if ($number == $checkNum) {
    echo "$number is a Palindrome number";
} else {
    echo "$number is not a Palindrome number";
}
?>
                                      

OUTPUT

Enter the number: 494
494 is a Palindrome number