Friday 27 March 2020

Shell program to find the sum of odd digits and even digits from a number.

PROGRAM
echo "enter a number"
read n
os=0
es=0
while test $n -gt 0
do
  r=`expr $n % 10`
  if test `expr $r % 2` -eq 0
  then
     es=`expr $es + $r`
  else
     os=`expr $os + $r`
  fi
  n=`expr $n / 10`
done
echo "sum of odd digits in the number = $os"
echo "sum of even digits in the number = $es"


OUTPUT
enter a number
12345
sum of odd digits in the number = 9
sum of even digits in the number = 6

No comments:

Post a Comment