Headlines
Loading...
Program in UNIX to calculate the sum of digits

Program in UNIX to calculate the sum of digits

If a three-digit positive number is input through the keyboard, write a program to calculate the sum of digits.

echo "Enter a three digit number : "
read num

if  [ $num -gt 999 -o $num -lt 0 ]
then
    echo "Invalid number"
    exit
fi

temp=$num
sum=0

while [ $num -ne 0 ]
do
r=`expr $num % 10`
sum=`expr $sum + $r`
num=`expr $num / 10`
done
echo "Sum of digits of $temp is $sum"
*** PLEASE checkout the Best deals from for top sites like Amazon, Flipkart etc ***