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

 

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

Given two ints a and b, add every ODD number between a and b and MULTIPLY by every even number between a and b. So if a is 4 and b is 9, the result should be 305, the result of (5*6+7)*8+9. Notice that the multiplication operation relies on the running total thus far. Notice also that if a is even, it will not be part of the output. For all inputs, a < b is a precondition (a is guaranteed to be less than b for all inputs). Remember that the remainder operator % can be used to find the remainder during division. When you divide an odd number by 2, the remainder is 1.

HINT
You need a loop to do this.

apcsaLoopsAddMultiplyInclusive(4, 9) → 305
apcsaLoopsAddMultiplyInclusive(5, 8) → 296
apcsaLoopsAddMultiplyInclusive(-10, -4) → 1580

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

public int apcsaLoopsAddMultiplyInclusive(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