Written by Sai Sandeep Thota on April 15, 2013 in Programming > Shell

# Shell Script to Find Sum of Digits

This shell script is used to find the sum of Digits. If the given number is 23 then the sum will be 2+3 = 5. The script is explained below:

Example:

Given Number is: 23.

Then n=23.

**First Iteration:**

sd=3

n=2

sum=2

—–

**Second Iteration:**

sd=2

n=0

sum=5

echo "Enter a Number:"
read n
temp=$n
sd=0
sum=0
while [ $n -gt 0 ]
do
sd=$(( $n % 10 ))
n=$(( $n / 10 ))
sum=$(( $sum + $sd ))
done
echo "Sum is $sum"

Output:

*Related*

Sai Sandeep has written **107** articles for us.

Programming. Networking. Photography.
You can follow me on : Facebook | Twitter | Google+ | Instagram

{ 0 comments… add one now }