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

 

orion.a.smith@gmail.com apcsa-loops > apcsaLoopsAddInclusive
prev  |  next  |  chance

Given two ints a and b, add all the numbers between a and b, inclusive. So if a is 4 and b is 7, the result should be 22, the result of 4+5+6+7. Precondition: a < b (a is guaranteed to be less than b for all inputs).


HINTS

Think about the pieces of a for loop; the declaration and initialization of the loop control variable, the condition and the update.  Does the loop control variable have to start at 0 every time?  Supposing that the condition is some kind of inequality, are there choices about what kind of inequality to use?

About adding number sequences: There are several non-looping ways to solve this problem and all the ones that follow it, which you shouldn't use the first time you solve them.  Doing this in various non-looping ways is described at the following page:

https://betterexplained.com/articles/techniques-for-adding-the-numbers-1-to-100/

apcsaLoopsAddInclusive(4, 7) → 22
apcsaLoopsAddInclusive(5, 8) → 26
apcsaLoopsAddInclusive(-10, -4) → -49

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

public int apcsaLoopsAddInclusive(int a, int b) { }

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

Copyright Nick Parlante 2017 - privacy