Shell Script to print sum of all digits of a given number - The Coding Shala

Home >> Scripting >> Sum of all digits

Shell Script to print sum of all digits of a given number

Write a shell script to print the sum of all digits of a given number?

Code: 

#!/bin/bash
n=$1
sum=0
tmp=0
while [ $n -gt 0 ]
do
tmp=`expr $n % 10`
sum=`expr $sum + $tmp`
n=`expr $n / 10`
done
echo "sum of digits is $sum"



Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Binary Number with Alternating Bits LeetCode Solution - The Coding Shala

Time Complexity, Space Complexity, Asymptotic Notations - The Coding Shala

Java Program to Find GCD or HCF of Two Numbers - The Coding Shala

Java Program to Reverse a String using Stack - The Coding Shala

Sort an Array According to Count of 1(set) bits - The Coding Shala