-->

Shell Script to add the digits of a number

Posted by Admin on
Write script to print given numbers sum of all digit, For eg. If no is 123 it's sum of all digit will be 1+2+3 = 6.

#!/bin/ksh

n=123
num=0
while [ n -gt 0 ]
do
rev=`expr $n % 10`
num=`expr $rev + $num`
n=`expr $n / 10`
done

echo "Sum of digits is $num "


No comments:

Post a Comment