Monday 9 July 2012

Write a shell script to check whether a given number is an Armstrong number or not.


echo "Enter a number: "
read c

x=$c
sum=0
r=0
n=0
while [ $x -gt 0 ]
do
r=`expr $x % 10`
n=`expr $r \* $r \* $r`
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

--------------------------------------------------------------------------------------------------------------
                OUTPUT
Enter a number:
153
It is an Armstrong Number.

15 comments:

Unknown said...

thank you so much ...!! :)
it's helpful

SUKANTA CHAKRABORTY said...

Thnx...realy its very help ful

Crazy Guy said...

In this program if i give a four digit number or a number less than 3 digits, for eg. If i give input as 2 then it will give output as not armstrong, which is wrong. This program will only work for 3 digit numbers. So junior get your concept cleared first.

Unknown said...

there are six Armstrong numbers in the range of 0 and 999.
Armstrong number 1: 0
Armstrong number 2: 1
Armstrong number 3: 153
Armstrong number 4: 370
Armstrong number 5: 371
Armstrong number 6: 407

Panku said...

pls give code of lcm of two no. in cygwin shell script

Unknown said...

Đơn vị vận chuyển gửi hàng từ việt nam sang Hoa Kỳ
Công ty vận chuyển gửi hàng từ việt nam sang Lôn-đôn chi phí rẻ
Đơn vị ship nhanh hàng hóa từ việt nam sang thái lan chi phí thấp

Unknown said...

2 is armstrong number?

Unknown said...

its really useful...and very simple...thankyou

Unknown said...

Yes obvio

Unknown said...

Sorry but their are some error why?

Unknown said...

Okay

Anonymous said...

GOOD

IT Quiz said...

good one . Cyber Security Quiz

Unknown said...

wrong

Unknown said...

i've tried many code, but yours work nice and easy

Post a Comment