Java Reference
In-Depth Information
while (counter <= nthFibonacci)
{
current = previous2 + previous1;
previous1 = previous2;
previous2 = current;
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.
COMPLETE PROGRAM LISTING
//*************************************************************
// Author: D.S. Malik
//
// Program: nth Fibonacci number
// Given the first two numbers of a Fibonacci sequence, this
// determines and outputs the desired number of the Fibonacci
// sequence.
//*************************************************************
import javax.swing.JOptionPane;
public class FibonacciNumber
{
public static void main (String[] args)
{
//Declare variables
String inputString;
String outputString;
int
previous1;
int
previous2;
int
current = 0;
int
counter;
int
nthFibonacci;
inputString =
JOptionPane.showInputDialog("Enter the first "
+ "Fibonacci number: ");
//Step 1
previous1 = Integer.parseInt(inputString);
//Step 2
inputString =
JOptionPane.showInputDialog("Enter the second "
+ "Fibonacci number: "); //Step 3
previous2 = Integer.parseInt(inputString);
//Step 4
Search WWH ::




Custom Search