Reverse number

Reverse number

A number can be written in reverse order.

For example

12345 = 54321

Logic:

  • Declare a variable to store reverse number and initialize it with 0.
  • Multiply the reverse number by 10, add the remainder which comes after dividing the number by 10.

Reversing Number in PHP

Example:

Below progrem shows digits reversal of 23456.

<?php  
$num = 23456;  
$revnum = 0;  
while ($num > 1)  
{  
$rem = $num % 10;  
$revnum = ($revnum * 10) + $rem;  
$num = ($num / 10);   
}  
echo "Reverse number of 23456 is: $revnum";  
?>  
<?php  
function reverse($number)  
{  
   /* writes number into string. */  
    $num = (string) $number;  
    /* Reverse the string. */  
    $revstr = strrev($num);  
    /* writes string into int. */  
    $reverse = (int) $revstr;   
     return $reverse;  
}  
 echo reverse(23456);  
?>  

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments