Question: HOMEWORK 2 2 : Recursive Sum of Digits Write a function that is passed a number and returns the sum of its digits using recursion.

HOMEWORK 22: Recursive Sum of Digits
Write a function that is passed a number and returns the sum of its digits using recursion. For example:
\table[[Sum of Digits],[Sum of Digits,]]
input: 21 output: 3
input: 687 output: 21
I'll give you a hint on the logic. The Mod operator can be a powerful tool. Let think through an example, let's say that n=523, and we have 2 additional variables, temp and sum. This function could be easily executed with a Do-While Loop, but you must use recursion to get credit. (VBA)
rirst pass through the recursive loop
temp =n Mod 10 'temp =523810=3
sum = sum + temp ??=0+3=3
n=n-temp, n=523-3=520
n=n10'n=52010=52
[do the reqursive part]
1 second pass through the recursive loop
temp =nMod10 'temp =52&10=2
sum = sum + temp ??=3+2=5
n=n-tempquadnn=52-2=50
n=n??10quad'n=50??10=5
[dotherecursi]vepart
[teII the code to stop nov]
 HOMEWORK 22: Recursive Sum of Digits Write a function that is

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!