Logo 
Search:

Unix / Linux / Ubuntu Forum

Ask Question   UnAnswered
Home » Forum » Unix / Linux / Ubuntu       RSS Feeds

shell scripting

  Asked By: Zubair    Date: Apr 16    Category: Unix / Linux / Ubuntu    Views: 1853
  

Write a shell script which gets digits in a single variable and prints the sum of all digits for example, Input : 123 output 3+2+1=6

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Kirtiranjan Sahoo     Answered On: Aug 07

#!bin/bash

read -p "Enter a number : " n
sum=0
for (( i=1;i<=n;i++ ))
do
dig=$(( $n % 10 ))
sum=$(( $sum + $dig ))
n=$(( $n / 10 ))
done
echo "The sum of digits are : $sum"

 
Didn't find what you were looking for? Find more on shell scripting Or get search suggestion and latest updates.




Tagged: