Tuesday, 3 November 2015

Linux Shell program function to find the sum of digits of a number

function sum()
{
echo "enter n"
read n
sum=0
while [ $n -gt 0 ]
do
sum=`expr $sum + $n % 10 `
n=`expr $n / 10`
done
echo -n "sum of digits:"
echo $sum
}
sum


OUTPUT:
enter n 231
sum of digits:6

No comments:

Post a Comment