# PHP Program to print the reverse of a number

March 15, 2022, Learn eTutorial
1802

## What is the reverse of a number?

In this program, we are reversing the number using PHP. Reversing the number is meant by printing the user inputted number in reverse order. For example, if the user entered number is 567 then the output of the program will be 765.

## How to find the reverse of a number in PHP?

To find the reverse of the number entered by the user in PHP first we have to read the number into a variable then we have to perform the operations first we have to find the Mod(%) of the number and assign it to the variable rem then we have to multiply by 10 with the variable rev and add the current value in the variable rem to it and assign it to the variable rev and after that divide the number by 10 to avoid the last digit of the number and assign the number into the variable number iterate these operations using `while loop` until every digit of the variable is reversed. And after the completion of the iteration print the value of the variable rev as the reverse of the number.

### ALGORITHM

Step 1: Accept the number from the user which wants to be reversed into a variable number

Step 2: Assign the current number from the variable number to num

Step 3: Assign the value 0 into variable rev to store the reverse of the number

Step 4: Perform the following sub-steps until the condition 'num > 1' becomes false

(i) Perform the operation 'num % 10' and assign the value to the variable rem to store the remainder

(ii) Perform the operation 'rev * 10 + rem' and assign the value to the variable rev to store in reverse

(ii) Perform the operation 'num / 10' and assign the value to the variable num to store the number without the last

Step 5: Print variable rev as the reverse of the number entered by the user in variable num

## PHP Source Code

```                                          ```<?php
\$number = readline("Enter the number: ");
\$num = \$number;
\$rev = 0;
while (\$num > 1) {
\$rem = \$num % 10;
\$rev = (\$rev * 10) + \$rem;
\$num = (\$num / 10);
}
echo "Reverse number of \$number is: \$rev";
?>```
```

## OUTPUT

```Enter the number: 9687
Reverse number of 9687 is: 7869```

VIEW ALL
VIEW ALL