Calculating Sum of Digits in a Number using Array Formulas [for fun]
Here is a fun formula to write.
Given a number in cell, I want you to find the sum of digits in it. So, for eg. if you have the number 3584398594 in a cell, the sum would be =3+5+8+4+3+9+8+5+9+4, equal to 1994. 😛
I am kidding of course, the sum would be 58.
Now, how would you write a formula to find this sum automatically based on the number entered in the cell?
Go ahead and figure it out. If you can, come back and check your answer with mine below.
How to get the sum of digits?
In order to get the sum of digits, we just need to separate and add all the numbers. Sounds simple right? But how!?!
Very simple, we use Array formulas and pixie dust.
First the formula:
Assuming the number is in cell B4, we write the formula,
to get the sum of digits.
Note: you need not press CTRL+SHIFT+Enter to get this formula work.
How does this formula work?
We will go inside out to understand this formula.
The portion –
ROW(OFFSET($A$1,,,LEN(B4))): Gives the numbers 1,2,3…n where n is the number of digits of the value in B4.
The portion –
MID(B4,ROW(OFFSET($A$1,,,LEN(B4))),1): Now gets the individual digits of the number and returns them as array (since the 2nd argument of MID formula is an array.
The SUMPRODUCT: is the pixie dust. It just magically sums up all the digits extracted by MID(). We use a +0 at the end because MID() returns text that needs to be converted to numbers for SUMPRODUCT to work its magic.
How would you have solved this?
I just love SUMPRODUCT Formula. So I use it whenever I can. But you may like other techniques. So please tell me how you would solve this problem using formulas. Post your formula using comments.
Note: while posting your formula, just put it between CODE tags like this:
<code>your formula goes here</code> so that it gets displayed correctly.
Bonus question: How to calculate single digit sum of the digits?
Go ahead and solve it too.
The single digit sum is arrived by summing the sum of digits of sum of digits of … of a number. For ex. the single digit sum for number 3584398594 is 4 (because the sum of digits is 58, whose sum of digits is 5+8 = 13, whose sum of digits is 1+3 =4 and we stop here because 4 is a single digit number).
More Formula Fun:
- Find if a number is prime or not using array formulas
- Simulating dice throws using Excel
- Shuffling a list of numbers using formulas
- Learn Excel Array Formulas – Examples, Tips & Tricks
Sign-up for our FREE Excel tips newsletter:
Here is a smart way to become awesome in Excel. Just signup for my FREE Excel tips newsletter. Every week you will receive an Excel tip, tutorial, template or example delivered to your inbox. What more, as a joining bonus, I am giving away a 25 page eBook containing 95 Excel tips & tricks. Please sign-up below:
Your email address is safe with us. Our policies
Leave a Reply
|Use Analytical Charts to Make your Boss Love You||Announcing Dashboard Week – Submit your entries now!|