Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a recursive function called sumDigits with the following signature: public static long sumDigits(long n) that computes the sum of the digits in a number

Write a recursive function called sumDigits with the following signature:

public static long sumDigits(long n)

that computes the sum of the digits in a number repeatedly, until the sum is a single digit. For example, if we call sumDigits(123456), the following would result:

sumDigits(123456) => 1+2+3+4+5+6 => 21 => 2+1 => 3

so the final answer would be 3. You must meaningfully use recursion for this lab in order to receive any credit.

Hint: For this problem it might be useful to convert back and forth between longs and strings. This can be done using the Long.parseLong method and the Long.toString method.

Note: This problem is based on the Recursive Sum Digit problem on Hackerrank, but it has been simplified. If you find this lab relatively straightforward, I recommend you take a look at that problem.

https://www.hackerrank.com/challenges/recursive-digit-sum

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

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions