{"id":2249,"date":"2017-12-08T05:43:14","date_gmt":"2017-12-08T05:43:14","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/?p=2249"},"modified":"2020-01-09T09:28:44","modified_gmt":"2020-01-09T09:28:44","slug":"write-script-to-print-given-numbers-sum-of-all-digit","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/write-script-to-print-given-numbers-sum-of-all-digit\/","title":{"rendered":"Write script to print given numbers sum of all digit,"},"content":{"rendered":"<p><strong>rajeshkumar created the topic: Write script to print given numbers sum of all digit,<\/strong><br \/>\nWrite script to print given numbers sum of all digit, For eg. If no is 123 it&#8217;s sum of all digit will be 1+2+3 = 6.<\/p>\n<p><code>#!\/bin\/bash<br \/>\n#<br \/>\n#<br \/>\n# Algo:<br \/>\n#       1) Input number n<br \/>\n#       2) Set sum=0, sd=0<br \/>\n#   \t3) Find single digit in sd as n % 10 it will give (left most digit)<br \/>\n#       4) Construct sum no as sum=sum+sd<br \/>\n#       5) Decrment n by 1<br \/>\n#       6) Is n is greater than zero, if yes goto step 3, otherwise next step<br \/>\n#       7) Print sum<br \/>\n#<br \/>\nif [ $# -ne 1 ]<br \/>\nthen<br \/>\n    echo \"Usage: $0   number\"<br \/>\n    echo \"       I will find sum of all digit for given number\"<br \/>\n    echo \"       For eg. $0 123, I will print 6 as sum of all digit (1+2+3)\"<br \/>\n    exit 1<br \/>\nfi<\/p>\n<p>n=$1<br \/>\nsum=0<br \/>\nsd=0<br \/>\nwhile [ $n -gt 0 ]<br \/>\ndo<br \/>\n    sd=`expr $n % 10`<br \/>\n    sum=`expr $sum + $sd`<br \/>\n    n=`expr $n \/ 10`<br \/>\ndone<br \/>\n    echo  \"Sum of digit for numner is $sum\"<\/p>\n<p>#<br \/>\n# .\/ch.sh: vivek-tech.com to nixcraft.com referance converted using this tool<br \/>\n# See the tool at http:\/\/www.nixcraft.com\/uniqlinuxfeatures\/tools\/<br \/>\n#<\/code><\/p>\n<p>Regards,<br \/>\nRajesh Kumar<br \/>\nTwitt me @ <a href=\"http:\/\/twitter.com\/RajeshKumarIn\" target=\"_blank\" rel=\"noopener\">twitter.com\/RajeshKumarIn<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>rajeshkumar created the topic: Write script to print given numbers sum of all digit, Write script to print given numbers sum of all digit, For eg. If no is 123&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[454],"tags":[278],"class_list":["post-2249","post","type-post","status-publish","format-standard","hentry","category-shell-script","tag-script"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=2249"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2249\/revisions"}],"predecessor-version":[{"id":2250,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2249\/revisions\/2250"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=2249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=2249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=2249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}