Palindrome Number In PHP – simple and easy method

Write a program to find the number is Palindrome number in PHP

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.

Let’s check how to program palindrome number in php.

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

I hope this tutorial for checking palindrome number in php is helpful for you.

Click here for the program factorial in php

Click here for more knowledge on Php

Related Posts

forest fire

Forest Fire – Techgig, All testcases passed. Challenge #1

Forest FireĀ (100 Marks) Challenge 1: Forest fire You are camping in a forest area at night. You are living with the forest officers to experience their challenges…

the magic wand

The Magic Wand – TechGig PHP answer, all test cases passed. Challenge #2

The Magic Wand – TechGig PHP answer – all testcases passed The Magic Wand (100 Marks) You are a wizard who possesses the magic wand that can be…

PHP interview Questions and Answers

What is PHP? Answer: PHP stands for Hypertext Preprocessor. It is a widely used server-side scripting language primarily designed for web development. PHP code is embedded within…

install php

A Step-by-Step Guide on How to Install PHP

Install PHP : A Step-by-Step Guide on How to Install PHP Introduction: PHP is a widely used server-side scripting language that powers countless websites and web applications….

what is php

What is PHP? Understanding PHP: The Powerhouse of Web Development

Introduction: What is PHP? Understanding PHP: The Powerhouse of Web Development: In the world of web development, PHP stands tall as one of the most widely-used and…

php form

Complete Php form example

Here we are going to check how to create a php form and insert its values to database table. In this example we will be submitting the…

Leave a Reply

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