Java Reference
In-Depth Information
1. Display the input dialog box to prompt the user for the first Fibo-
nacci number—that is,
previous1
. Store the string representing
previous1
into
inputString
.
2. Retrieve the string from
inputString
, and store the first Fibonacci
number into
previous1
.
3. Display the input dialog box to prompt the user for the second
Fibonacci number—that is,
previous2
. Store the string represent-
ing
previous2
into
inputString
.
4. Retrieve the string from
inputString
, and store the second Fibo-
nacci number into
previous2
.
5. Create the
outputString
and append
previous1
and
previous2
.
6. Display the input dialog box to prompt the user for the desired
Fibonacci number, that is,
nthFibonacci
. Store the string repre-
senting
nthFibonacci
into
inputString
.
7. Retrieve the string from
inputString
, and store the desired nth
Fibonacci number into
nthFibonacci
.
MAIN
ALGORITHM
5
8.
a.
if
(nthFibonacci == 1)
the desired Fibonacci number is the first Fibonacci number.
Copy the value of
previous1
into
current
.
b.
else if
(nthFibonacci == 2)
the desired Fibonacci number is the second Fibonacci number.
Copy the value of
previous2
into
current
.
c.
else
calculate the desired Fibonacci number as follows:
Since you already know the first two Fibonacci numbers of the
sequence, start by determining the third Fibonacci number.
i. Initialize
counter
to
3
, to keep track of the calculated
Fibonacci numbers.
ii. Calculate the next Fibonacci number, as follows:
current = previous2 + previous1;
iii. Assign the value of
previous2
to
previous1
.
iv. Assign the value of
current
to
previous2
.
v.
Increment
counter
.
Repeat Steps 8c(ii)
through 8c(v) until
the Fibonacci
number you want is calculated.
The following
while
loop executes Steps 8c(ii) through
8c(v) and determines the nth Fibonacci number:
Search WWH ::
Custom Search