FibonacciNumber

FibonacciNumber - + "the Fibonacci sequence:...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
//Program: nth Fibonacci number / import javax.swing.JOptionPane; i 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 outputString = "The first two numbers of the " + "Fibonacci sequence are: " + previous1 + " and " + previous2; //Step 5 inputString = JOptionPane.showInputDialog("Enter the position " + "of the desired number in "
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: + "the Fibonacci sequence: "); //Step 6 nthFibonacci = Integer.parseInt(inputString); //Step 7 if (nthFibonacci == 1) //Step 8.a current = previous1; else if (nthFibonacci == 2) //Step 8.b current = previous2; else //Step 8.c { counter = 3; //Step 8.c.1 //Steps 8.c.2 - 8.c.5 while (counter <= nthFibonacci) { current = previous2 + previous1; //Step 8.c.2 previous1 = previous2; //Step 8.c.3 previous2 = current; //Step 8.c.4 counter++; //Step 8.c.5 } } outputString = outputString + "\nThe " + nthFibonacci + "th Fibonacci number of " + "the sequence is: " + current; //Step 9 JOptionPane.showMessageDialog(null, outputString, "Fibonacci Number", JOptionPane.INFORMATION_MESSAGE); //Step 10 System.exit(0); } }...
View Full Document

Page1 / 2

FibonacciNumber - + "the Fibonacci sequence:...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online