Loading…
OS · Loops & numbers · Program 11 · In the record
Write the shell script to print the sum of the digit of the 3 digit number
1#!/bin/bash2 3echo "Enter a 3 digit number:"4read n5 6if (( n < 100 || n > 999 ))7then8 echo "Please enter a 3 digit number"9 exit 110fi11 12d1=$((n / 100))13d2=$((n / 10 % 10))14d3=$((n % 10))15 16sum=$((d1 + d2 + d3))17 18echo "$d1 + $d2 + $d3 = $sum"19echo "Sum of digits of $n = $sum"456Enter a 3 digit number: 456 4 + 5 + 6 = 15 Sum of digits of 456 = 15
Run program 11 in your browser
bash · just-bash. Change the code or the input, run it, break it, and see what happens.