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.

MainClass.java - contains main() method.

SumDigits.java - contains calculateSumOfDigits() method in which business logic is written.

Step 2: Variables required in MainClass.java class

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

Variables required in SumDigits.java 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 SumDigits.java class is calculateSumOfDigits() in which actual business logic is written.

Step 4: Actual code,

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

MainsumOfDigits

 

In SumDigits.java 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.

SumDigits

Following is its execution.

ResultSumOfDigits

 


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

Comments (0)

There are no comments posted here yet

How many if you were caught by your superior/boss/teacher while sleeping/ taking a nap in the office hours?

People tend to sleep during office hours. Some do it apparently when they are tired after some hard work, while some do it on routine manner. This poll is only for fun to see how many belong to various categories.

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 4 answer(s).
/polls/funny-polls/6698-how-many-if-you-were-caught-by-your-superior-boss-teacher-while-sleeping-taking-a-nap-in-the-office-hours.json?task=poll.vote
6698
radio
[{"id":"23352","title":"Yes, I was caught red handed","votes":"0","type":"x","order":"1","pct":0,"resources":[]},{"id":"23353","title":"yes, I was caught but I managed to cover it","votes":"1","type":"x","order":"2","pct":14.29,"resources":[]},{"id":"23354","title":"No I wasn't caught so far","votes":"0","type":"x","order":"3","pct":0,"resources":[]},{"id":"23355","title":"I never sleep during my office hours","votes":"6","type":"x","order":"4","pct":85.71,"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