Reverse Number Program in PHP – Tutorialpath

Welcome to all our readers on our website at www.tutorialpath.com. As you all know that here on our website we will provide you lots of information related to the latest and newly introduced technology which helps you to get updated with the latest tech. Here you will get all the useful information which is required in this tech world. As you know technology is one of the trending parts of this world. So, here again, we came up with the latest and new tech information which is related to the process of creating a Reverse Number Program in PHP. With the help of this article, you will get to know the complete process of how to create a Reverse Number Program in PHP in a step by step manner. So, simply have a look at this article and grab all the useful information which is going to be very helpful for you.

Write a program to reverse digits of a number

This is a basic PHP program that describes how to write a Reverse Number program in PHP.

Reverse Number:-

Given an integer number and we need to find its reverse number using PHP

Basic Example:- 

Input number is: 98765

Reverse number will be: 56789

  1. <?php
  2. $num = 98765;
  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 98765 is: $revnum”;

?>

Output:

Reverse number of 98765 is: 56789

Logic

  • Take (input or assign) a number
  • Declare a variable to store reverse number and initialize it with 0.
  • Run the following two steps until number is not zero
  • Get the digit using modules (%) operator and add it into the ‘reverse*10’
  • Now, divide the number by 10
  • Finally, when number will be zero, print the ‘reverse’

Example 2:- Reverse Number Program in PHP With Example

  1. <?php
  2. if(isset($_POST[‘submit’]))
  3. {
  4. $rev=0;
  5. $num=$_POST[‘num’];
  6. while($num>=1)
  7. {
  8. $re=$num%10;
  9. $rev=$rev*10+$re;
  10. $num=$num/10;
  11. }
  12. echo “Reverse number of is ” .$rev;
  13. }

?>

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Reverse of a number</title>
  5. </head>
  6. <body>
  7. <form name=”reversenumber” action=”” method=”post”>
  8. Number :<input type=”text” name=”num” value=”” required=””><br>
  9. <input type=”submit” value=”Submit” name=”submit”>
  10. </form>
  11. </body>
  12. </html>

Conclusion

So, hopefully we have shared all the basic steps that help you Reverse Number Program in PHP.  PHP extract the last character from a string. In this tutorial, you have several ways for PHP to remove the last character from any given strings.

 Recommended Posts:

  • Write a Program to print whether a year leap year or not
  • PHP program to print identity matrix
  • Formula based PHP program to find the sum of all divisors of n
  • PHP Program to find the sum of even index term
  • PHP Program to add two numbers without using arithmetic operator
  • PHP Program of finding nth Delannoy Number
  • Dynamic Programming solution for subset sum problem
  • How to Print Triangle of Stars in PHP
  • Print Triangle of Character Pattern in PHP

PHP date_format() function with example

Leave a Reply

Your email address will not be published. Required fields are marked *