Pages

Write a Program to Find Sum of Digits in Java

One of the common programming practice question thrown to beginners is to write a program to calculate the sum of digits in an integral number. For example, if the input is 123456 then output or sum of the digit is (1+2+3+4+5+6) = 21. An 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 it's 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 the 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.

Java program to find Sum of Digits in Java

Here is our complete Java program to solve this problem. As explained in first paragraph, it does not use any library method instead uses division and modulus operator to calculate sum of digits of a number.

Please enter a number to calculate sum of digits
101Sum of digits of number 101 is 2Please enter a number to calculate sum of digits
123Sum of digits of number 123 is 6

That's all on how do you calculate sum of digits of a number in Java. It's an interesting exercise and you are welcome to find other solution as well. Try not to see the solution before doing it because if you can come up with logic by your own, you will learn a lot. These kind of programs are good for learning basic programming techniques and developing coding sense. If you are interested, you can find lot of such questions in this blog e.g. checkout this 10 programming questions article.

I don't think it's necessary for you to use the four classes you've imported from the java.util.concurrent package, because that is more for parallel programming. You got multiple threads trying to access a common element at the same time, but you would use those classes to avoid thread locks. In addition, I can't seem to understand why you need to import java.io.Console, when the System class is already imported.

Anyway, very good article. What do you say you check out my Blogger post? Suggestions are welcome! http://gregorypdesrosiersmontreal.blogspot.com/2014/06/rock-paper-scissors-lizard-spock-java.html

Hello @Anonymous and @Gregory, those imports are not warranted in this program. It seems they are just left from another exercise and some how I forgot to remove them. Anyway, thanks for pointing that.