Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Problem with my algorithm...

Greenhorn
Posts: 15
Alright, I've been having some trouble with this. Basically, I need to read in 2 integers (for example, 10 and 15), then multiply them like so: a * a + 1 * ... b. So, for our example, we would need to multiply 10 x 11 x 12 x 13 x 14 x 15 and output the answer.

Here's what I have so far:

I'm not sure how to go about creating the proper algorithm to do the rest, though. I know I need to use either a While or For loop, but I have had no luck with getting the right algorithm down. Any help is appreciated, thanks.

author & internet detective
Sheriff
Posts: 38027
605
Zack,
You can use either loop to do this. "for" is a bit easier as you get the loop index built in. There are four parts to a loop. Think about what each part should be and it will help form the algorithm here.
1) When should the loop start
2) When should the loop end
3) How should I increment/decrement the loop?
4) What should I do in the loop?

Feel free to a post a follow up question or what you think the four parts should be.

(instanceof Sidekick)
Ranch Hand
Posts: 8791
If you're not at all familiar with loops, here's the most common form:

All the questions Jeanne suggested are answered there. See if you can match up a tiny fragment of that code with each of her questions.

Marshal
Posts: 58367
178
That is a variant of the recursive factorial problem.

Sheriff
Posts: 21288
87
Please don't mention recursion when a simple loop can do the trick as well. PLEASE!

Rich Nelson
Greenhorn
Posts: 15
I feel stupid. I still can't figure out what exactly needs to be done to solve this problem.

I know I need to use a loop (a for loop) and I know how to use them, but I can't figure out the logical mathematics behind the problem.

Rich Nelson
Greenhorn
Posts: 15
Ok...I made this WAY too hard for myself.

Thanks for the help, everyone.

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
Hey, glad you fought through to the end!