Example: #1 - Check the number is a Palindrome
Check the number is a Palindrome using a divide and modulus approach
PHP Input Screen
<?php
class PalindromeExample {
public function checkPalindrome() {
$originalNumber = 12321;
$reverseNumber = 0;
$temp = $originalNumber;
while ($temp > 0) {
$remainder = $temp % 10;
$reverseNumber = ($reverseNumber * 10) + $remainder;
$temp = (int)($temp / 10);
}
if ($originalNumber == $reverseNumber) {
echo("The given number is Palindrome");
} else {
echo("The given number is NOT a Palindrome");
}
}
}
$obj = new PalindromeExample();
$obj->checkPalindrome();
PHP Output Screen
The given number is Palindrome