Itβs our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.
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
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.where should i read up on it? Any best material,book, site?
You should really read up on it if you don't understand the concept. Challenges are not going to be enough.
function sumDigits( n)
if n = 1
return 1
else
return LAST + sumDigits( REST)
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
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:
Open in new window
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.