Hi Can any one please post the answer for the above program..................
banta.sh 28462
banta.sh:
#!/bin/ksh
a="${1}"
typeset -i i=1
while (( i <= 5 ))
do
((num=$(echo "${a}" | cut -c "${i}") ))
if (( i == 1 )); then
printf "$num"
else
printf "+$num"
fi
((sum=sum + num))
((i=i+2))
done
printf "=${sum}\n"
Python alternative
#!/usr/bin/python
a = 12345
b = list(str(a))
print sum( map(int,b[::2]) )
output:
9
You need to initialise the variables num and sum
#!/bin/ksh
a=12346
b=`expr length $a`
echo " length : $b"
typeset -i i=1
num=0
sum=0
while (( i <= $b ))
do
((num=$(echo "${a}" | cut -c "${i}") ))
if (( i == 1 )); then
printf "$num"
else
printf "+$num"
fi
((sum=sum + num))
((i=i+2))
done
printf "=${sum}\n"
~
Answer
-------
length : 5
1+3+6=10
Er... obvious example of homework? Thread closed.