your image

PHP Reverse Number Program - javatpoint

javapoint
Related Topic
:- PHP Java information technology

Reverse number

A number can be written in reverse order.

For example

12345 = 54321

Logic:

 

Features of Java - Javatpoint

  • 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.

  1. <?php  
  2. $num = 23456;  
  3. $revnum = 0;  
  4. while ($num > 1)  
  5. {  
  6. $rem = $num % 10;  
  7. $revnum = ($revnum * 10) + $rem;  
  8. $num = ($num / 10);   
  9. }  
  10. echo "Reverse number of 23456 is: $revnum";  
  11. ?>  

Output:

Reversing Number With strrev () in PHP

Example:

Function strrev() can also be used to reverse the digits of 23456.

  1. <?php  
  2. function reverse($number)  
  3. {  
  4.    /* writes number into string. */  
  5.     $num = (string) $number;  
  6.     /* Reverse the string. */  
  7.     $revstr = strrev($num);  
  8.     /* writes string into int. */  
  9.     $reverse = (int) $revstr;   
  10.      return $reverse;  
  11. }  
  12.  echo reverse(23456);  
  13. ?>  

Output:/strong>

Comments