Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# sumDigits  challenge

Posted on 2016-09-09
Medium Priority
182 Views
Hi,

http://codingbat.com/prob/p163932

I am working on above challenge
Given a non-negative int n, return the sum of its digits recursively (no loops). Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).

sumDigits(126) â†’ 9
sumDigits(49) â†’ 13
sumDigits(12) â†’ 3

i was not sure how to find the sum of digits recursively without loop which i worked earlier. please advise
0
Question by:gudii9
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2
• 2

LVL 27

Expert Comment

ID: 41792111
Do you remember how to break up an integer into the LAST digit and the REST using % and / .

Recursion works by letting a function call itself multiple times, each time with a smaller or simpler argument.

The pseudo code would be something like:
``````function sumDigits( n)
sum = 0
if arg = 0
return sum
else
sum = sum + LAST( arg) + sumDigits( REST( arg))
``````

RECURSION is probably the single most important and elegant topic in computer science.
You should really read up on it if you don't understand the concept.  Challenges are not going to be enough.
0

LVL 7

Author Comment

ID: 41792185
Do you remember how to break up an integer into the LAST digit and the REST using % and / .
yes
RECURSION is probably the single most important and elegant topic in computer science.
You should really read up on it if you don't understand the concept.  Challenges are not going to be enough.
where should i read up on it? Any best material,book, site?
0

LVL 27

Accepted Solution

d-glitch earned 1000 total points
ID: 41792669
Here are a couple of references:
http://introcs.cs.princeton.edu/java/23recursion/
http://arxiv.org/pdf/cs/9301113

My pseudo code needs some mods:
``````function sumDigits( n)
if n = 1
return 1
else
return  LAST + sumDigits( REST)
``````
0

LVL 28

Assisted Solution

rrz earned 1000 total points
ID: 41793107
My pseudo code is a little different from d-glitch
if input is 0 then return 0
if not then return last + sumDigits(rest)
0

LVL 7

Author Comment

ID: 41793534
I will try
0

LVL 7

Author Comment

ID: 41793651
``````public int sumDigits(int n) {
if (n < 10)
{return n;
}
return (n % 10) + sumDigits(n/10);
}
``````

above passes all test. any improvements or alternate approaches?
0

LVL 28

Expert Comment

ID: 41793722
if (n < 10)
{return n;
Brilliant! I like your base case much better.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to goâ€¦
###### Suggested Courses
Course of the Month9 days, 10 hours left to enroll