Quantcast
Channel: Interview Archives - Mydons
Viewing all articles
Browse latest Browse all 32

How to reverse a number in php without built in functions

$
0
0

php_logo_128

A Simple PHP Home work code that returns the reverse of a given number,
<php
  $num = 1234;  $rem =0; $rev =0;
  $originalnum = $num;
  while($num > 1) {
      $rem = $num %10;
      $rev = ($rev * 10)+ $rem;
      $num = $num /10;

 }
echo "Original Number: ". $originalnum;
echo "
 Reverse : ". $rev;
>
The Logic here is simple. First get the given number and extract the final digit. Initially the reverse of the number will be zero, on each iteration multiply the reverse with 10 and add the modulus result.

Viewing all articles
Browse latest Browse all 32

Trending Articles