0% found this document useful (0 votes)
32 views

Sum of Digit

This code takes a number as input, calculates the sum of its digits, and outputs the result. It first reads in a number, then uses a for loop to iterate through each digit by taking the input modulo 10, adds each digit to a running sum, and divides the number by 10 to remove the rightmost digit after each iteration, until the number is reduced to zero and all digits have been summed.

Uploaded by

VedasEverywhere
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Sum of Digit

This code takes a number as input, calculates the sum of its digits, and outputs the result. It first reads in a number, then uses a for loop to iterate through each digit by taking the input modulo 10, adds each digit to a running sum, and divides the number by 10 to remove the rightmost digit after each iteration, until the number is reduced to zero and all digits have been summed.

Uploaded by

VedasEverywhere
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

echo "enter no"

read n
sum=0
length="${#n}"
for ((i=0;i<length;i++))
do
d=$((n%10))
sum=$((sum+d))
n=$((n/10))
done
echo "sum of digit for given no is : $sum"

You might also like