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

 

test2025_04_17_SL_digitAltSum


Write a method that returns the sum of all the digits in 'num' except that the SIGN of each digit alternates in such a way that the ones place is positive, the tens place is negative, the hundreds place is positive, and so on. For example, if 'num' is 5718, then the alternating sum will be (-5) + 7 + (-1) + 8 = 9. Notice that the ones place is always positive, so the alternating begins with the least significant digit. Look at the test data for examples. As a precondition, you may assume that 'num' is positive.


test2025_04_17_SL_digitAltSum(5718) → 9
test2025_04_17_SL_digitAltSum(91929394) → -26
test2025_04_17_SL_digitAltSum(919293940) → 26

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

public int test2025_04_17_SL_digitAltSum(int num) { }

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: 250

Copyright Nick Parlante 2017 - privacy