Given a number Num, find the sum of digits of the number.
Examples:
Input :
444
Output :
sum of digits of 444 is : 12
Input :
34
Output :
sum of digits of 34 is : 7
Approach:
1. Divide the number into single digits
2. Find the sum of digits .
Bash
Num=123
g=$Num
s=0
while [ $Num -gt 0 ]
do
k=$(( $Num % 10 ))
Num=$(( $Num / 10 ))
s=$(( $s + $k ))
done
echo "sum of digits of $g is : $s"
|
Output:
sum of digits of 123 is : 6
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
18 Aug, 2021
Like Article
Save Article