Home » PHP

PHP program to check whether given number is palindrome or not

Here, we are implementing a PHP program that will check whether a given integer number is palindrome or not.
Submitted by IncludeHelp, on January 03, 2018

Given a number and we have to check whether it is palindrome or not using PHP program.

Palindrome number

A number which is equal to its reverse number is said to be a palindrome number.

Example:

    Input:
    Number: 12321

    Output:
    It is palindrome number

    Explanation:
    Number is 12321 and its reverse number is 12321, 
    both are equal. Hence, it is a palindrome number.

Program:

<?php
    //function: isPalindrome
    //description
    function isPalindrome($number){
        //assign number to temp variable
        $temp = $number;
        //variable 'sum' to store reverse number
        $sum = 0;
        
        //loop that will extract digits from the last
        //to make reverse number
        while(floor($temp)){
            $digit = $temp % 10;
            $sum = $sum*10 + $digit;
            $temp = $temp/10;
        }
        //if number is equal to its reverse number
        //then it will be a palindrome number
        if($sum == $number)
            return 1;
        else
            return 0;
    }
    
    //Main code to test above function
    $num = 12321;
    if(isPalindrome($num))
        echo($num . " is a palindrome number");
    else
        echo($num . " is not a palindrome number");
?>

Output

12321 is a palindrome number





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.