Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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]
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 11 The Database For Big Data And Analytics

Authors: Cristian Molaro, Surekha Parekh, Terry Purcell, Julian Stuhler

1st Edition

1583473858, 978-1583473856

More Books

Students also viewed these Databases questions

Question

What are the various types of investments?

Answered: 1 week ago

Question

Describe ERP and how it can create efficiency within a business

Answered: 1 week ago