Here, we will figure out how to turn around an integer number in PHP? This post contains PHP code with output and clarification.
Given an integer number and we need to discover, print its turn around number utilizing PHP.
Example
The information number is: 12345
Turn around number will be: 54321
Rationale
- Take (enter or relegate) a number
- Dole out 0 to the variable, in which we are going to store turn around number (Here, I am utilizing ‘switch’)
- Run the accompanying two stages until the number isn’t zero
- Get the digit utilizing modules (%) administrator and include it into the ‘reverse*10’
- Presently, partition the number by 10
- At last, when the number will be zero, print the ‘turn around’
PHP code to turn around an integer number
<?php
//input
$num = 12345;
//reverse should be 0
$reverse = 0;
//run loop until num is not zero
while ($num != 0)
{
$reverse = $reverse * 10 + $num % 10;
$num = (int)($num / 10);
}
/printing the reverse number
echo "Reverse number of 12345 is: $reverse";
?>
Output