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:

# Fibonacci numbers problem

Greenhorn
Posts: 1
Here is the problem:

The Fibonacci numbers Fn are defined as follows. F0 is 1, F1 is 1, and
Fi+2 = Fi + Fi+1

i = 0, 1, 2, ... . In other words, each number is the sum of the previous two numbers. The first few Fibonacci numbers are 1, 1, 2, 3, 5, and 8. One place where these numbers occur is as certain population growth rates. If a population has no deaths, then the series shows the size of the population after each time period. It takes an organism two time periods to mature to reproducing age, and then the organism reproduces once every time period. The formula applies most straightforwardly to asexual reproduction at a rate of one offspring per time period. In any event, the green crud population grows at this rate and has a time period of five days. Hence, if a green crud population starts out as 10 pounds of crud, then in five days there is still 10 pounds of crud; in ten days there is 20 pounds of crud, in fifteen days 30 pounds, in twenty days 50 pounds, and so forth. Write a program that takes both the initial size of a green crud population (in pounds) and a number of days as input, and outputs the number of pounds of green crud after that many days. Assume that the population size is the same for four days and then increases every fifth day. Your program should allow the user to repeat this calculation as often as desired.

Here is my code:

I'd really appreciate some guidance on this, I lost myself in the for statement and I'm not sure what to do next.

Edit by mw: Added closing code tag.
[ October 04, 2007: Message edited by: marc weber ]

Sheriff
Posts: 11343
Welcome to JavaRanch!

Here are some tips...

First, for(int i=5; i==5; i++) will execute the loop body only once (because i==5 is only true the first time). I think you want this loop to iterate for the number of days that the user inputs.

Next, "size" references a String, so size + size is a concatenated String. Instead, I think you want to work with the numeric value, "initialSize."

In doing this, I don't think you don't want x + x. Instead, you want x + (previous x). So you might consider introducing another variable to hold the previous value.

See what you come up with based on that.

Ranch Hand
Posts: 103
um - yes. Your for loop starts at 5 and ends at 5. I assume that this is where you want your calculation; but by putting your prompt in here as well, it looks like you're mixing up the calculation with the prompt for new starting values.

I don't think your problem is with the Java code - it might go better if you figured out first how you'd do it "by hand". For instance, first you ask the user for input; then you calculate his answer; then you give him his answer; then (separately) you ask him if he wants you to do another calculation.

Then the calculation belongs in the for loop that you've started; but right now you aren't using the user's time period input in the loop at all.

I sometimes find it's easier if I'm stuck to get the logic right using pencil and paper, and then worry about how to translate that into code.

Good luck!

 Montana has cold dark nights. Perfect for the heat from incandescent light. Tiny ad: Why should you try IntelliJ IDEA ? https://coderanch.com/wiki/696337/IntelliJ-IDEA