Code In Tech - The Ultimate World of Computer Technology

Saturday 12 May 2012

Shell script to sum of digits of a given no.

SOURCE CODE:

echo “Enter the no:”
read n
l =`expr length $n `
a=1
while [ $a –le $l ]
do
     r=`expr $n | cut –c $a`
     s=`expr $s + $r`
     a=`expr $a + 1`
done
echo “Ans:“ $s

OUTPUT:

Enter the no: 
123
Ans:6 

No comments :

Post a Comment