Bash Script to Check Armstrong Number with Output
sum=0; num=153; digits=${#num}; temp=$num; while [ $temp -gt 0 ]; do digit=$((temp % 10)); sum=$((sum + digit ** digits)); temp=$((temp / 10)); done; if [ $sum -eq $num ]; then echo "Armstrong number"; else echo "Not Armstrong number"; fi.Examples
How to Think About It
Algorithm
Code
read -p "Enter a number: " num sum=0 digits=${#num} temp=$num while [ $temp -gt 0 ]; do digit=$((temp % 10)) sum=$((sum + digit ** digits)) temp=$((temp / 10)) done if [ $sum -eq $num ]; then echo "$num is an Armstrong number" else echo "$num is not an Armstrong number" fi
Dry Run
Let's trace the number 153 through the code
Initialize variables
num=153, digits=3, sum=0, temp=153
First digit extraction and calculation
digit=153 % 10 = 3; sum=0 + 3^3=27; temp=153 / 10=15
Second digit extraction and calculation
digit=15 % 10 = 5; sum=27 + 5^3=27 + 125=152; temp=15 / 10=1
Third digit extraction and calculation
digit=1 % 10 = 1; sum=152 + 1^3=152 + 1=153; temp=1 / 10=0
Compare sum and original number
sum=153 equals num=153, so it is Armstrong
| Iteration | Digit | Sum | Temp |
|---|---|---|---|
| 1 | 3 | 27 | 15 |
| 2 | 5 | 152 | 1 |
| 3 | 1 | 153 | 0 |
Why This Works
Step 1: Count digits
We use ${#num} to find how many digits the number has, which is needed for the power calculation.
Step 2: Extract digits
Using modulo % 10 extracts the last digit, and integer division / 10 removes it.
Step 3: Sum powers
Each digit is raised to the power of the digit count using digit ** digits and added to sum.
Step 4: Compare result
If the sum equals the original number, it confirms the number is Armstrong.
Alternative Approaches
read -p "Enter a number: " num sum=0 digits=${#num} temp=$num while [ $temp -gt 0 ]; do digit=$((temp % 10)) power=$(echo "$digit^$digits" | bc) sum=$((sum + power)) temp=$((temp / 10)) done if [ $sum -eq $num ]; then echo "$num is an Armstrong number" else echo "$num is not an Armstrong number" fi
power() {
result=1
for ((i=0; i<$2; i++)); do
result=$((result * $1))
done
echo $result
}
read -p "Enter a number: " num
sum=0
digits=${#num}
temp=$num
while [ $temp -gt 0 ]; do
digit=$((temp % 10))
p=$(power $digit $digits)
sum=$((sum + p))
temp=$((temp / 10))
done
if [ $sum -eq $num ]; then
echo "$num is an Armstrong number"
else
echo "$num is not an Armstrong number"
fiComplexity: O(d) time, O(1) space
Time Complexity
The script loops once per digit of the number, so time grows linearly with the number of digits, O(d).
Space Complexity
Uses a fixed number of variables regardless of input size, so space complexity is O(1).
Which Approach is Fastest?
Using Bash's built-in ** operator is fastest; using bc or a custom function adds overhead.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Built-in ** operator | O(d) | O(1) | Fastest and simplest for modern Bash |
| bc tool | O(d) | O(1) | Compatibility with older Bash versions |
| Custom power function | O(d * p) | O(1) | No external tools, more control |
${#num} to get the number of digits in a Bash variable easily.