Write a program to display Reverse of any number in PHP ? Last Updated : 07 Oct, 2021 Comments Improve Suggest changes Like Article Like Report Write a program to reverse the digits of an integer. Examples : Input : num = 12345 Output: 54321 Input : num = 876 Output: 678 It can be achieved using Iterative way or Recursive Way Iterative Method: Algorithm: Input: num (1) Initialize rev_num = 0 (2) Loop while num > 0 (a) Multiply rev_num by 10 and add remainder of num divide by 10 to rev_num rev_num = rev_num*10 + num%10; (b) Divide num by 10 (3) Return rev_num Example: num = 456213 rev_num = 0rev_num = rev_num *10 + num%10 = 3num = num/10 = 45621rev_num = rev_num *10 + num%10 = 20 + 6 = 31num = num/10 = 4562rev_num = rev_num *10 + num%10 = 260 + 5 = 312num = num/10 = 456rev_num = rev_num *10 + num%10 = 2650 + 4 = 3126num = num/10 = 45rev_num = rev_num *10 + num%10 = 2650 + 4 = 31265num = num/10 = 4rev_num = rev_num *10 + num%10 = 2650 + 4 = 312654num = num/10 = 0 Program: PHP <?php // Iterative function to // reverse digits of num function reversDigits($num) { $rev_num = 0; while($num > 1) { $rev_num = $rev_num * 10 + $num % 10; $num = (int)$num / 10; } return $rev_num; } // Driver Code $num = 456213; echo "Original number is :".$num; echo "\r\n"; echo "Reverse of no. is ", reversDigits($num); ?> OutputOriginal number is :456213 Reverse of no. is 312654 Time Complexity: O(log(n)), where n is the input number. Auxiliary Space: O(1) Recursive Method: PHP <?php // PHP program to reverse // digits of a number $rev_num = 0; $base_pos = 1; /* Recursive function to reverse digits of num*/ function reversDigits($num) { global $rev_num; global $base_pos; if($num > 0) { reversDigits((int)($num / 10)); $rev_num += ($num % 10) * $base_pos; $base_pos *= 10; } return $rev_num; } // Driver Code $num = 456213; echo "Original number is :".$num; echo "\r\n"; echo "Reverse of no. is ", reversDigits($num); ?> OutputOriginal number is :456213 Reverse of no. is 312654 Time Complexity: O(log(n)) where n is the input number. Comment More infoAdvertise with us Next Article Write a program to display Reverse of any number in PHP ? A akshitsaxenaa09 Follow Improve Article Tags : Web Technologies PHP PHP-math PHP-function PHP-Questions +1 More Similar Reads Program to print multiplication table of any number in PHP In this article, we will see how to print the multiplication table of any given number using PHP. To make the multiplication table, first, we get a number input from the user and then use for loop to display the multiplication table. We use HTML and PHP to display the multiplication table. The HTML 1 min read PHP Program to Print ASCII Value of all Digits of a Given Number Given a number, the task is to print ASCII value of all digits of a given number in PHP. ASCII values are often used to represent characters and digits. Sometimes, it's necessary to find the ASCII value of each digit in a given number, especially when dealing with character encoding or data manipula 3 min read PHP program to swap two numbers Integer values can be stored in a variable in PHP. It is easy to store and modify and swap these integer values using various mentioned approaches: Using a temporary variable: The value of the first number is stored in the temporary variable. This value is then replaced by the value of the second nu 4 min read How to find Sum the Digits of a given Number in PHP ? We will explore how to find the sum of the digits of a given number in PHP. This task involves extracting each digit from the number and adding them together to get the final sum. Table of Content Using a loop to extract digits one by oneUsing mathematical operations to extract digitsUsing a loop to 2 min read How to Convert a Given Number to Words in PHP? Given an integer N, your task is to convert the given number into words using PHP.Examples:Input: N = 958237764Output: Nine Hundred Fifty Eight Million Two Hundred Thirty Seven Thousand Seven Hundred Sixty FourInput: N = 5000Output: Five ThousandBelow are the approaches to convert a given number to 5 min read Like