Saturday, 16 July 2016

Write a shell script in Linux/Unix to find sum of Digits of a number.


echo "ENTER A NUMBER-"
read n
sum=0
while test $n -ne 0
do
dig=`expr $n % 10`
sum=`expr $sum + $dig`
n=`expr $n / 10`
done
echo "THE SUM OF DIGITS OF $n IS- $sum" 


OUTPUT:

ENTER A NUMBER- 255
THE SUM OF DIGITS OF 255 IS-12









2 comments:

Popular Posts