Introduction
This lab is designed to build experience using loops. In it, you'll make use of each of the "for" and "while" loops.
The Assignment
Please write a method that computes the nth number in the Fibinacci series. This method should use a for-loop to guide the computation of the requested number. This method should have the signature below:
- public static int fibonacci (int n) // Note: n starts at 0
Please also write a method that gets an integer from the user. This should now be old hand:
Lastly, please construct a main() method as follows:
The Fibonacci Series
The Fibonacci Series is quite famous for its appearance in many biological, mathematical, and other systems. It is computed as follows:
- F0 = 1
- F1 = 1
- Fn = Fn-1 + Fibn-2, n > 1
Below is the beginning of the series:
- 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Computing the values
To compute this in your program, you'll want to handle n=0 and n=1 as special cases and immediately return 1. For the subsequent numbers, you'll want to use a for loop to do the computation.
You should initialize variables nMinusOne and nMinusTwo, compute the next value, and then shift nMinusOne into nMinusTwo and then the newly computed value into nMinusOne. You should repeat this process until you've compued the nth value, as requested.
As usual...