0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Check Armstrong Number with Output

Use a Bash script that calculates the sum of each digit raised to the power of the number of digits and compares it to the original number, like 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

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 an Armstrong number, count how many digits it has, then for each digit, raise it to the power of that count and add all these values together. If the total equals the original number, it is an Armstrong number.
📐

Algorithm

1
Get the input number.
2
Count the number of digits in the number.
3
Initialize sum to zero.
4
Extract each digit from the number.
5
Raise the digit to the power of the number of digits and add to sum.
6
Compare sum with the original number and print the result.
💻

Code

bash
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
Output
Enter a number: 153 153 is an Armstrong number
🔍

Dry Run

Let's trace the number 153 through the code

1

Initialize variables

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

2

First digit extraction and calculation

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

3

Second digit extraction and calculation

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

4

Third digit extraction and calculation

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

5

Compare sum and original number

sum=153 equals num=153, so it is Armstrong

IterationDigitSumTemp
132715
251521
311530
💡

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

Using bc for power calculation
bash
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
Uses external bc tool for power, useful if Bash version lacks ** operator; slightly slower but more compatible.
Using a function to calculate power
bash
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"
fi
Defines a custom power function for better control; avoids external tools but more code.

Complexity: 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.

ApproachTimeSpaceBest For
Built-in ** operatorO(d)O(1)Fastest and simplest for modern Bash
bc toolO(d)O(1)Compatibility with older Bash versions
Custom power functionO(d * p)O(1)No external tools, more control
💡
Use ${#num} to get the number of digits in a Bash variable easily.
⚠️
Forgetting to update the temporary variable inside the loop causes an infinite loop.