Write a Shell Bash Script for check if a provided number is Armstrong or not
#!/usr/bin/env bash | |
echo -n "Enter A Number: " | |
read -r n | |
arm=0 | |
temp=$n | |
while [ "$n" -ne 0 ]; do | |
r=$((n % 10)) | |
arm=$((arm + r * r * r)) | |
n=$((n / 10)) | |
done | |
echo $arm | |
if [ $arm -eq "$temp" ]; then | |
echo "Armstrong" | |
else | |
echo "Not Armstrong" | |
fi |
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND
#Your’s is wrong and works only for 3 digits but this one works flawlessly
#!/bin/bash
read -p “Enter Number:” num
temp=$num
arm=0
count=${#num}
if [ $num -lt 10 ]
then
echo “$num is armstrong number (1-9)”
else
while [ $num -gt 0 ]
do
rem=$((num%10))
arm=$((arm + rem**count))
num=$((num/10))
done
if [ $arm -eq $temp ]
then
echo “$arm = $temp Armstrong Number!”
else
echo “$arm != $temp Not a Armstrong Number!”
fi
fi