Fibonacci Number in Java

The following Java program application is a Fibonacci number. I will be using the JCreator IDE in developing the program. To start in this tutorial, first open the JCreator IDE, click new and paste the following code
  1. import javax.swing.JOptionPane;
  2.  
  3. public class FibonacciNumber
  4. {
  5. public static void main (String[] args)
  6. {
  7. //declaration of variables
  8. String inputString;
  9. String outputString;
  10. int previous1;
  11. int previous2;
  12. int current = 0;
  13. int counter;
  14. int nthFibonacci;
  15.  
  16. inputString =
  17. JOptionPane.showInputDialog("Enter the first Fibonacci number: ");
  18. previous1 = Integer.parseInt(inputString);
  19.  
  20. inputString =
  21. JOptionPane.showInputDialog("Enter the second Fibonacci number: ");
  22. previous2 = Integer.parseInt(inputString);
  23.  
  24. outputString = "The first two numbers of the"
  25. + "Fibonacci sequence are: "
  26. + previous1 + "and" + previous2;
  27. inputString =
  28. JOptionPane.showInputDialog("Enter the position of the desired "
  29. + "number in the Fibonacci sequence: ");
  30. nthFibonacci = Integer.parseInt(inputString);
  31.  
  32. if (nthFibonacci == 1) //copies the value of previous1 into current
  33. current = previous1;
  34. else if (nthFibonacci == 2)//copies the value of previous2 to current
  35. current = previous2;
  36. else //calculate the desired Fibonacci number
  37. {
  38. counter = 3;
  39. while (counter <= nthFibonacci)
  40. {
  41. current = previous2 + previous1;
  42. previous1 = previous2;
  43. previous2 = current;
  44. counter++;
  45.  
  46. }
  47. }
  48. outputString = outputString + "\nThe "
  49. + nthFibonacci
  50. + "th Fibonacci of "
  51. + "the sequence is: "
  52. + current;
  53.  
  54. JOptionPane.showMessageDialog(null, outputString,
  55. "Fibonacci Number",
  56. JOptionPane.INFORMATION_MESSAGE);
  57. System.exit(0);
  58. }
  59. }
Sample Run: The program works as follow: The statement
  1. inputString =
  2. JOptionPane.showInputDialog("Enter the first Fibonacci number: ");
  3. previous1 = Integer.parseInt(inputString);
prompts the user for the first Fibonacci number that is, previous1 and it stores into inputString. The statement
  1. inputString =
  2. JOptionPane.showInputDialog("Enter the second Fibonacci number: ");
  3. previous2 = Integer.parseInt(inputString);
display the input dialog box to prompt the user for the second Fibonacci number. The statement
  1. outputString = "The first two numbers of the"
  2. + "Fibonacci sequence are: "
  3. + previous1 + "and" + previous2;
create output and append previous1 and previous2. The statement
  1. inputString =
  2. JOptionPane.showInputDialog("Enter the position of the desired "
  3. + "number in the Fibonacci sequence: ");
  4.  
  5. nthFibonacci = Integer.parseInt(inputString);
displays the input dialog box to prompt the user for the desired Fibonacci number and store the nth Fibonacci into inputString. The statement
  1. JOptionPane.showMessageDialog(null, outputString,
  2. "Fibonacci Number",
  3. JOptionPane.INFORMATION_MESSAGE);
display the output dialog box showing the first two and nth Fibonacci number.

Add new comment