0
0
PhpProgramBeginner · 2 min read

PHP Program to Check Armstrong Number

A PHP program to check an Armstrong number uses a loop to sum the powers of each digit (raised to the number of digits) and compares the sum with the original number, like if ($sum == $number) { echo 'Armstrong'; }.
📋

Examples

Input153
Output153 is an Armstrong number
Input9474
Output9474 is an Armstrong number
Input123
Output123 is not an Armstrong number
🧠

How to Think About It

To check if a number is Armstrong, split the number into its digits, raise each digit to the power of the number of digits, sum these values, and then compare the sum to the original number. If they match, the number is Armstrong.
📐

Algorithm

1
Get the input number.
2
Count the number of digits in the number.
3
Initialize sum to zero.
4
For each digit in the number, raise it to the power of the digit count and add to sum.
5
Compare sum with the original number.
6
Return if the number is Armstrong or not.
💻

Code

php
<?php
$number = 153;
$sum = 0;
$digits = strlen((string)$number);
$temp = $number;

while ($temp > 0) {
    $digit = $temp % 10;
    $sum += pow($digit, $digits);
    $temp = (int)($temp / 10);
}

if ($sum == $number) {
    echo "$number is an Armstrong number";
} else {
    echo "$number is not an Armstrong number";
}
?>
Output
153 is an Armstrong number
🔍

Dry Run

Let's trace 153 through the code

1

Initialize variables

number=153, sum=0, digits=3, temp=153

2

First loop iteration

digit=153 % 10 = 3; sum=0 + 3^3=27; temp=153 / 10=15

3

Second loop iteration

digit=15 % 10 = 5; sum=27 + 5^3=27 + 125=152; temp=15 / 10=1

4

Third loop iteration

digit=1 % 10 = 1; sum=152 + 1^3=152 + 1=153; temp=1 / 10=0

5

Compare sum and number

sum=153, number=153; they are equal

IterationDigitSum after powerTemp after division
132715
251521
311530
💡

Why This Works

Step 1: Count digits

We use strlen to find how many digits the number has, which is needed to raise each digit to the correct power.

Step 2: Sum powers of digits

Each digit is extracted using modulus and raised to the power of the digit count using pow, then added to the sum.

Step 3: Compare sum to original

If the sum equals the original number, it confirms the number is Armstrong, otherwise it is not.

🔄

Alternative Approaches

Using string manipulation
php
<?php
$number = 9474;
$digits = strlen((string)$number);
$sum = 0;
foreach (str_split($number) as $digit) {
    $sum += pow($digit, $digits);
}
if ($sum == $number) {
    echo "$number is an Armstrong number";
} else {
    echo "$number is not an Armstrong number";
}
?>
This method uses string functions and foreach loop, which can be easier to read but may be slightly slower for very large numbers.
Recursive approach
php
<?php
function armstrongSum($num, $digits) {
    if ($num == 0) return 0;
    $digit = $num % 10;
    return pow($digit, $digits) + armstrongSum((int)($num / 10), $digits);
}
$number = 153;
$digits = strlen((string)$number);
$sum = armstrongSum($number, $digits);
if ($sum == $number) {
    echo "$number is an Armstrong number";
} else {
    echo "$number is not an Armstrong number";
}
?>
This recursive method is elegant but uses more memory and can be slower due to function calls.

Complexity: O(d) time, O(1) space

Time Complexity

The program loops through each digit once, so time grows linearly with the number of digits, O(d).

Space Complexity

Only a few variables are used regardless of input size, so space complexity is constant, O(1).

Which Approach is Fastest?

The iterative method using modulus and division is fastest and uses least memory compared to string or recursive methods.

ApproachTimeSpaceBest For
Iterative modulusO(d)O(1)Fastest and memory efficient
String manipulationO(d)O(d)Easier to read, good for beginners
RecursiveO(d)O(d)Elegant but uses more memory
💡
Use strlen on the number converted to string to get digit count easily.
⚠️
Forgetting to raise digits to the power of the number of digits, which is essential for Armstrong check.