write a shell program that allows to enter a number and it find the sum of odd positioned digits and even positioned digits from the entered numbers

example:

enter the number: 123456

the sum of odd digits is :9

the sum of even digits is :12


program:-

echo "enter"
read num
rev=0
even=0
odd=0
while [ $num -gt 0 ]
do
tmp=$(( $num % 10 ))
if(( $tmp % 2 == 0 ))
then
even=$(( $even + $tmp ))
else
odd=$(( $odd + $tmp ))
fi
rev=$(( $rev * 10 + $tmp ))
num=$(( $num / 10 ))
done
echo the sum of even number $even
echo the sum of odd number $odd


output:-







1 Comments

If you have any doubts, Please let me know
Thanks!

Post a Comment

If you have any doubts, Please let me know
Thanks!

Previous Post Next Post