I wrote this code in order to sum the digits of any string, and to also create a loop that stops the summing process when the length of the sum is equal to a single digit.
What's wrong?
n = '12361927'function d = sumdigits(n) s=0 for i=1:length(n) s=s+str2double(n(i)) endends=sumdigits(n)if length(str2num(s))==1 s=selse while length(str2num(s))~=1 if length(str2num(s))==1 break else s=sumdigits(s) end end end
Best Answer