Shell script program to find the sum of digit

Here I shared the simple shell script program to find the sum of digit, that take a number as input and we add the each digit with other digit using loop and finally displaying the value to console.

For example you entered 2234 as a input, now sum of digit will be calculated like 4+3+2+2 and the result will be 11. To get last digit we divide the given number by 10 and remainder will contain the last digit, this digit is added to get the sum of digit in program

Shell script program to find the sum of digit

Here is complete shell script program to find the sum of digit in linux/unix.

echo “==============SUM OF DIGIT===========”

echo “Enter a number”

read num

temp=$num

sum=0

sd=0

while [ $num -gt 0 ]

do

sd=$(( $num %10 ))

num=$(( $num/10 ))

sum=$(( $sum+$sd ))

done

echo “Sum of Digit $temp is: $sum”

Leave a comment