Loading…
OS · Loops & numbers · Program 06 · In the record
Write a shell script to print the sum of the digit of the input term.
1#!/bin/bash2 3echo "Enter a number:"4read n5 6num=$n7sum=08 9while (( num > 0 ))10do11 sum=$((sum + num % 10))12 num=$((num / 10))13done14 15echo "Sum of digits of $n = $sum"4567Enter a number: 4567 Sum of digits of 4567 = 22
Run program 06 in your browser
bash · just-bash. Change the code or the input, run it, break it, and see what happens.