MATLAB Answers

Sum the digits of a number?

486 views (last 30 days)
tzenh karetzh
tzenh karetzh on 17 Jan 2014
Commented: Les Beckham on 3 May 2020
Hi, I'd like to know how can someone add the digits of a number to the final point in matlab.
For example 525 --> 5 + 2 + 5 = 12 --> 1 + 2 = 3
I'm thinking about dividing by ten and adding the digits after the decimal point while at the same time round the number.
Any ideas on how to add the digits would be really helpfull.
Also how to identify a digit by knowing its position in a number
i.e. the 5th digit of 9483672 is 6. Thanks in advance

  2 Comments

José-Luis
José-Luis on 17 Jan 2014
Is this homework?
tzenh karetzh
tzenh karetzh on 17 Jan 2014
Not exactly.My homework is to find the prime numbers in a certain space. This is just a question that came to me because we know that numbers like 711 that their digits add up to 3,6 or 9 can be divided by 3.For the prime numbers it's much easier to go for mod(number,3). And maybe it can be useful to someone else for other use

Sign in to comment.

Answers (5)

Azzi Abdelmalek
Azzi Abdelmalek on 17 Jan 2014
a=525;
b=num2str(a);
while numel(b)>1
a=sum(str2double(regexp(b,'\d','match')));
b=num2str(a);
end
out=str2num(b)
% -------------------------------
a=9483672;
b=num2str(a);
b(5)

  2 Comments

rohan dhane
rohan dhane on 15 Nov 2019
you are fanta.......
Stephen Cobeldick
Stephen Cobeldick on 15 Nov 2019
Simpler without regexp and str2double:
a = 525;
b = num2str(a);
while numel(b)>1
a = sum(b-'0');
b = num2str(a);
end
out = a;

Sign in to comment.


Les Beckham
Les Beckham on 2 May 2020
Edited: Les Beckham on 2 May 2020
I know it sounds too easy to be true but this manipulation is actually the same as modulo 9. No loops or string conversions needed.
>> mod(525,9)
ans =
3
>> mod(9483672,9)
ans =
3
For the second part of your question, try this:
function [out] = extract_digit(num,digit)
%EXTRACT_DIGIT Return the specified digit from a number
out = num2str(num);
out = out(digit);
end

  2 Comments

Stephen Cobeldick
Stephen Cobeldick on 3 May 2020
Neat idea, needs special-case handling for 9 itself.
Les Beckham
Les Beckham on 3 May 2020
Good catch. I forgot about that special case.

Sign in to comment.



Be Matlabi
Be Matlabi on 12 Jul 2018
Edited: Be Matlabi on 17 Jul 2018
If n is the Number whose digit have to be summed
n=521;
answer=0;
while(n>0)
k=mod(n,10);
answer=answer+k;
n=floor(k);
end
disp(answer);

  1 Comment

Syed Shahed
Syed Shahed on 2 May 2020
Though the logic is pretty clear ,the code is wrong.

Sign in to comment.


Pablo López
Pablo López on 5 Feb 2019
You can try this:
n = 525;
sum(str2num(num2str(sum(str2num(num2str(n)')))'))

  4 Comments

Show 1 older comment
Pablo López
Pablo López on 6 Feb 2019
Yes, I've tested it and it works correctly since 9483672 = 9+4+8+3+6+7+2 = 39 = 3+9 = 12
And if you enter the following code:
n = 9483672;
sum(str2num(num2str(sum(str2num(num2str(n)')))'))
The result is ans = 12
Hope that helps!
Stephen Cobeldick
Stephen Cobeldick on 6 Feb 2019
The question gave this example:
"For example 525 --> 5 + 2 + 5 = 12 --> 1 + 2 = 3"
which indicates that the 12 digits should also be summed to 3. Usually when this task is given, it is required to continue summing until only one digit is reached (and this is what the other answers do). In contrast, your code sums twice only, regardless of how many digits remain. This may or may not be the intended behavior, it depends entirely on the specifications requested by the assignment. I just wanted to note the distinction.
Pablo López
Pablo López on 6 Feb 2019
Well seen! Thank you for your observation.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by