PHP Program to check whether the number Armstrong or Not

What is an Armstrong Number?

It is necessary to know what an Armstrong number means in order to write this basic PHP program. The Armstrong number is a number whose sum of the cubes of the total digits will be equal to the number itself. For example, we can take a number 371, and to check that number is Armstrong number or not, we first have to take the cube of and 7 and 1 and add those cubes together and check whether the result is equal to 371 or not using an if condition in PHP. If the result is also 371, then it is an Armstrong number. Otherwise, it won't be. Here 27+343+1 = 371, so it is an Armstrong number.

How to check whether a number is an Armstrong number or Not in PHP?

In PHP to check whether a number is Armstrong or Not first, we have to read the input from the user and assign it to a variable. Then we have to split the digits of the number using the mod(%) operator by 10 and find the cube of each digit and add the results into a variable. After that, we have to compare the obtained result and the entered value if both values are equal then we can sort out that the entered number is an Armstrong number. Else if the entered number is not equal to the obtained result then the number won't be an Armstrong number.


Step 1: Accept the value from the user into the variable num

Step 2: Assign the value 0 into the variable total

Step 3: Perform the following sub-step until the condition 'x != 0' becomes false

            (i) Assign the computed result of 'x % 10' into the variable rem

            (ii) Assign the computed result of 'total + rem * rem * rem' into the variable total

            (iii) Assign the computed result of 'x / 10' into the variable x

Step 4: Check the condition 'num == total' if it is true then print 'Yes num is an Armstrong number' otherwise print 'No num is not an Armstrong number' using echo

PHP Source Code

$num = readline("Enter the number: ");
$total = 0;
$x = $num;
while ($x != 0) {
    $rem = $x % 10;
    $total = $total + $rem * $rem * $rem;
    $x = $x / 10;
if ($num == $total) {
    echo "Yes $num is an Armstrong number";
} else {
    echo "No $num is not an armstrong number";


Enter the number: 407
Yes 407 is an Armstrong number