Shell program to read a number and find the sum of digits

#!/bin/bash
# Shell program to read a number and find the sum of digits
# -----------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
echo -n "Enter number : "
read n
 
# store single digit
sd=0
 
# store number of digit 
sum=0
 
# use while loop to caclulate the sum of all digits
while [ $n -gt 0 ]
do
    sd=$(( $n % 10 )) # get Remainder 
    n=$(( $n / 10 ))  # get next digit
    sum=$(( $sum + $sd )) # calculate sum of digit
done
echo  "Sum of all digit  is $sum"
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN CentOS 8 Debian 10 Firewall Ubuntu 20.04
10 comments… add one
  • komakan Mar 17, 2017 @ 15:58

    Programs

  • mahsa Nov 7, 2012 @ 7:01

    please write shell script to find sum of first $ last digit of number

    • ShanthaKumar Jun 3, 2013 @ 13:34

      very late, but here is it ;)

      $ read a && echo $(echo $a | grep -o . | head -1) + $(echo $a | grep -o . | tail -1) |bc

  • kalpana May 28, 2012 @ 10:00

    is this the shell program to find the sum of digits of a number until a single digit is obtained?

  • Poo May 11, 2012 @ 6:30

    Can any1 temme how to do shell program to find the sum of digits of a number until a single digit is obtained ?

  • ruchika Mar 31, 2012 @ 6:55

    shell script that reads a filename from the command line and changes the name to filename.aa.where aa is the logname of the user

  • ruchika Mar 31, 2012 @ 6:53

    a shell script to calculate the sum of digits of a 5 digit number which is input through the keyboard

  • jadu saikia Dec 28, 2008 @ 6:24

    A one liner for sum of digits using sed.

    $ expr $(echo "12334" | sed 's/[0-9]/ + &/g' | sed 's/^ +//g')

    13

    • ShanthaKumar Jun 3, 2013 @ 13:41

      echo -n “Sum of the digits is ” && echo $( echo $1 | grep -o . | tr “n” “+” && echo “0”) | bc

  • basant kumar Aug 26, 2008 @ 16:07

    please provide more shell script for unix reader..

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.