Click here to Skip to main content
15,882,163 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
You have to find the sum of the individual digit sum of all the elements in the array

The digit sum here refers to the sum of digits, present in a given element



What I have tried:

i have able to find total sum except individual
Posted
Updated 30-Jun-22 21:55pm
Comments
Richard MacCutchan 1-Jul-22 5:39am    
You need to split each number into its digits and add them together. Take the remainder by repeatedly dividing by 10 to get all the digits.

Quote:
i have able to find total sum except individual
In order to obtain the sum of the individual digits of a number the integer division and the relative reminder are your friends.
For example, if you have n=35, then 3 is the quotient of n divided by 10 (integer division), while 5 is the reminder, and the sum of digits is 3+5=8 (you may iterate the procedure for numbers having more digits).
See, for instance: Integer Division in JavaScript | Delft Stack[^].
 
Share this answer
 
Comments
Patrice T 1-Jul-22 6:09am    
+5
CPallini 1-Jul-22 6:15am    
Thank you, but... Your solution looks more clear to me.
Have my 5 as well.
Patrice T 1-Jul-22 6:34am    
thank you
Quote:
You have to find the sum of the individual digit sum of all the elements in the array

Googling "get digits from integer" gives only 21 millions of answers, lets guess you can find useful in them.
The key is to use an integer division
I = T * 10 + U where Integer is made of 10 times Tens plus Unit.
Or T is result of integer division of I by 10, and U is reminder of division.
You need to search for integer division and reminder of integer division (modulo).
 
Share this answer
 
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900