The Artima Developer Community
Sponsored Link

Java Buzz Forum
Write a Program to Find Sum of Digits in Java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Javin Paul

Posts: 1090
Nickname: javinpaul
Registered: Jan, 2012

Javin Paul is Java Programmer working on Finance domain.
Write a Program to Find Sum of Digits in Java Posted: Jun 24, 2014 7:07 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Javin Paul.
Original Post: Write a Program to Find Sum of Digits in Java
Feed Title: Java67
Feed URL: http://www.java67.com/feeds/posts/default?alt=rss
Feed Description: Java and technology tutorials, tips, questions for all programmers.
Latest Java Buzz Posts
Latest Java Buzz Posts by Javin Paul
Latest Posts From Java67

Advertisement
One of the common programming practice question thrown to beginners is to write a program to calculate sum of digits in a integral number. For example, if input is 123456 then output or sum of digit is (1+2+3+4+5+6) = 21. Additional condition is you can not use any third party or library method to solve this problem. This program is not as simple as it looks and that's why its a good exercise, you must know some basic programming techniques e.g. loops, operators, and logic formation to solve this problem. Let's see how we can solve this problem using Java programming language. In order to calculate sum of digits we must get digits as numbers. So your first challenge is how do you get the digits as numbers?  How do we extract 6 out of 123456? If you have done exercises like palindrome check or reversing number, then you should know that there is very old technique of getting last digit from a number by using modulus operator. If we do 123456%10 then we will get 6, which is last digit. In order to get all digits we can use a loop, something like while loop. Now our next challenge is how do we reduce number in each iteration so that our loop will finish as soon as we are done with all digits of number? Now coming from same palindrome problem, you can use technique of dividing number by 10 to get rid of last digit or reduce it by factor of 10. For example 123456/10 will give you 12345, which is one digit less than original number. So you got your end condition for while loop, check until number is not equal to zero. These two techniques are very important and can be used in variety of problem, so always remember these.
Read more »

Read: Write a Program to Find Sum of Digits in Java

Topic: 5 tips to improve performance in Android applications Previous Topic   Next Topic Topic: Quicksort algorithm in Java – Code Example

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use