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.

6 comments:

Reshma sm Alam 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.

mahesh babu 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

Pankaj Giri said...

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

Đẩu Anh 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

Post a Comment