Write a program that does the following:
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.
9. Append the nth Fibonacci number to outputString. Notice that
the nth Fibonacci number is stored in current.
10. Display the output dialog box showing the first two and the nth Fibonacci
numbers.
Comments
Leave a comment