id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

jebbert@volusia.k12.fl.us > sumDigits
prev  |  next  |  chance

Write a method that accepts a string as an input parameter. The method finds all the digits in the string and adds them together. If that sum has more than one digit, find the sum of all the digits in that number. Keep doing this until the sum of the digits is a single digit. For example, "999j999999w999999u999999" has 21 9's which add up to 189. Since this has more than one digit, we will add those digits to get 18. But this still has more than one digit, so we add 1+8 to get 9. The final result is 9.


sumDigits("8124214124214214714298719287987421") → 4
sumDigits("8714298719287987421") → 5
sumDigits("182981247982417987241987214987241427189142214") → 4

...Save, Compile, Run (ctrl-enter)

public int sumDigits(String str) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 230

Copyright Nick Parlante 2017 - privacy