Problem Definition :- Write a java code to find out the sum of all digits of a given number.

Solution :-

Step 1: Classes that are required in coding. - contains main() method. - contains calculateSumOfDigits() method in which business logic is written.

Step 2: Variables required in class

readNumber of type integer - purpose is to read number from user during run time.

Variables required in class

sum and quotient, both of type integer.  Purpose of sum variable is to store sum of all digits and that of quotient is to store quotient when number is divided by 10. Why divide by 10, is   explained later

Step 3: Method required in class is calculateSumOfDigits() in which actual business logic is written.

Step 4: Actual code,

In, object sumDigits of a SumDigits class is created and by using object of Scanner class, required number is read.



In class, number that is read during run time is passed through calculateSumOfDigits() method. This number is divided by 10 to get one less digit (of unit's place) every time. Then the modulus is done on same number by using % with 10 which gives remainder and this remainder is same as that the number in unit's place of original number. And, ultimately that remainder is added every time when modulus is done. Here, remainder is stored in number variable and added with sum number. And then qoutient which was obtained using divide by 10, is again assigned to number variable for further division.


Following is its execution.



Like it on Facebook, Tweet it or share this article on other bookmarking websites.

Comments (0)

There are no comments posted here yet

iPhone 5 is worth for the price?

iPhone 5 is worth for the price? share your oponion  

No answer selected. Please try again.
Please select either existing option or enter your own, however not both.
Please select minimum 0 answer(s) and maximum 3 answer(s).
[{"id":"21061","title":"yes its worth","votes":"47","type":"x","order":"0","pct":44.34,"resources":[]},{"id":"21062","title":"No, not at all","votes":"32","type":"x","order":"0","pct":30.19,"resources":[]},{"id":"21063","title":"i don't know","votes":"27","type":"x","order":"0","pct":25.47,"resources":[]}] ["#ff5b00","#4ac0f2","#b80028","#eef66c","#60bb22","#b96a9a","#62c2cc"] ["rgba(255,91,0,0.7)","rgba(74,192,242,0.7)","rgba(184,0,40,0.7)","rgba(238,246,108,0.7)","rgba(96,187,34,0.7)","rgba(185,106,154,0.7)","rgba(98,194,204,0.7)"] 350
bottom 200
No married couple wants to end up getting divorced. It is not like they have planned for it. They try to put up with their partners for as long as they
Due to our modern lifestyle, we feel that digestion related disorders are a common problem. Thus, we neither give importance to them nor seek any help
The bond of marriage brings the two people together. Initially, everything may seem okay and both of them slowly start discovering each other in the journey.
Why to choose Java? In the computer market, being able to use Java is a crucial knowledge. Java is a development terminology used to build compelling
DATABASE CONNECTIVITY This articles explains the need of database connectivity, the way it is achieved through Java using the JDBC(java database connectivity)
  Write your first simple java program.   First of all we need is a placewhere we can write your Java code. All Java programs are written using
Array of objects in Java Arrays are defined as block of continous memory locations which are arranged in one after another. Elements of the Array can
Synchronized block is alternative mechanism for "synchronized" methods.If we inherihate the non synchronized blocks either from base class or interface