Bash Program to check if a number is Armstrong:Shell Scripting

Shell scripting is a command line scripting language used by Unix- Like environment. The below program is a Bash program to check if a number is Armstrong or not.

Whats is Armstrong number?

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.

–source https://pages.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/arms.html

Let’s understand what the code meant:

echo — echo command is used to print the characters on the screen, it echoes back whatever you type after it and strings must be enclosed in “” and variable must be start with $withvariablename. The $ sign tells the interpreter that the user wants to print the actual value stored in the variable.

read — read c command is reading an input and putting it in a variable named c, now that could be anything but we used c here.

function armstrong {} — This is actual function which consists the code for finding the armstrong number.

x=$1 —This tells the program to take the first parameter passed in the function and put it in the variable x.

l=${#x} — This is storing the number of digits in the number that we are passing. x is the variable contains the number that is passed in the function, # tell to count it, $ will take it’s value and put it in the variable l.

r=`expr $x % 10` — The math in shell scripting happens in the form of expr. Note that the expr expression is enclosed with the tick ( ` ) sign and it is not a quote. If you use a quote it will generate an error depending on the type of shell script.

n=$(bc <<< “$r^$l”) — expr does provide us with a lot of mathematical expression calculation ability but it does not provide us to calculate power of a number. So, To get the power of the number I’ve used bc command which is the best command for calculating different mathematical expression. The line suggests that, I am passing two variables $r,$l and using operator ^ which is power operator for bash and I am passing it to bc, which in turn storing the value in variable n.

while is the looping function, and If then else is conditional statements.

if [ $sum -eq $c ] — in bash as the operator we use -eq for equal to, -ne for not equal, -lt for less than, -gt for greater than instead of traditional <,>,==,!=.

The last two lines

result=`armstrong $c`

echo $result

says that armstrong function will take $c variable as the passed variable which will later becomes x inside the function and the result of the calculation of armstrong function will be saved in a variable called result.

Then we echo the result’s value.

CODE:
echo "Program to check armstrong number"
echo "Enter a number: "
read c
function armstrong {
x=$1
sum=0
r=0
n=0
l=${#x}
while [ $x -gt 0 ]
do
r=`expr $x % 10`

n=$(bc <<< "$r^$l")

sum=`expr $sum + $n`

x=`expr $x / 10`
done

if [ $sum -eq $c ]
then
echo "It is an Armstrong Number."
else
echo "It is not an Armstrong Number."
fi

}

result=`armstrong $c`
echo $result

To run the above code. Make a file name arm.sh and give it permission to run under your user using chmod u+x arm.sh. Once, it is done,run the program in the directory by ./arm.sh

OUTPUT:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: