So this is the actual question
Write a recursive function that computes the sum of the digits provided as an input argumenet. No need to check the input. Not allowed to use loops or string conversion like str2num. e.g. a number 12345 = 1+2+3+4+5 = 15. There is a hint: consider a mathematical operaion how to get the last digit of a base 10 number.
So far my plan is basically take the last digit of the input 12345
last digit =5
takeaway this value from the orignal number
12345-5=12340
divide by 10
12340/10=1234
add the last digit to the output
output=last digit + prev output
and recall the function till input = 0 at which point output the output
Below is what I currently have and being my first recursion problem quite holey. I am not sure of the base case but also if I define output as 0 then everytime I call the function the output will be 0, so I don't really know how to solve this
function [output]=digit_sum(input)a = mod(input,10) output=a+output input=input-a input=input/10 if input==0 output=output return else [output]=digit_sum(input) endend
Best Answer