PHP code to reverse an integer number

PHP code to reverse an integer number

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

  1. Take (enter or relegate) a number
  2. Dole out 0 to the variable, in which we are going to store turn around number (Here, I am utilizing ‘switch’)
  3. Run the accompanying two stages until the number isn’t zero
  4. Get the digit utilizing modules (%) administrator and include it into the ‘reverse*10’
  5. Presently, partition the number by 10
  6. 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

PHP code to reverse an integer number

Recent Posts

error: Alert: Content is protected!!