Difference between revisions of "Fibonacci sequence"
ChristopherJ (Talk | contribs) m (EDIT: No need for comma.) |
|||
Line 8: | Line 8: | ||
*prints out the n<sup>th</sup> fibonacci number, using [[JOptionPane_Methods|JOptionPane Methods]]. | *prints out the n<sup>th</sup> fibonacci number, using [[JOptionPane_Methods|JOptionPane Methods]]. | ||
<br> | <br> | ||
− | Note that the fibonacci sequence is F(0) = 1, F(1) = 1, F(n) = F(n-1) + F(n-2) for n > 1.<br> | + | Note that the fibonacci sequence is <math>F(0) = 1, F(1) = 1, F(n) = F(n-1) + F(n-2) for n > 1</math>.<br> |
Example: F(5) = 1, 1, 2, 3, 5, 8<br> | Example: F(5) = 1, 1, 2, 3, 5, 8<br> | ||
Your output should look something like<br> | Your output should look something like<br> | ||
Line 57: | Line 57: | ||
</pre> | </pre> | ||
− | The next step is declaring variables. In this program we will need three inegert variables. One variable will be used as F(n) and another variable will be used as F(n-1). Our initial value for F(n) will be 1 and F(n-1) will be 0, referring to the fibonacci formula. <br><br><b> | + | The next step is declaring variables. In this program we will need three inegert variables. One variable will be used as <math>F(n)</math> and another variable will be used as <math>F(n-1)</math>. Our initial value for <math>F(n)</math> will be 1 and <math>F(n-1)</math> will be 0, referring to the fibonacci formula. <br><br><b><math>F(0) = 1, F(1) = 1</math></b><br><br> |
Once you declare the necessary variables, the last variable will be the casted integer from the user input. | Once you declare the necessary variables, the last variable will be the casted integer from the user input. | ||
Line 75: | Line 75: | ||
</pre> | </pre> | ||
− | We will need another variable to keep track of F(n-2). You simply set it F(n-2) = F(n-1) as it starts a new iteration, and set F(n-1) = F(n). Then sum up the F(n-2) + F(n-1) which will now be F(n). | + | We will need another variable to keep track of <math>F(n-2)</math>. You simply set it <math>F(n-2) = F(n-1)</math> as it starts a new iteration, and set <math>F(n-1) = F(n)</math>. Then sum up the <math>F(n-2) + F(n-1)</math> which will now be <math>F(n)</math>. |
<pre> | <pre> |
Revision as of 10:07, 9 April 2010
Back to the Program-A-Day homepage
ProblemWrite a complete Java program titled Fibonacci that:
|
Mid-term Review
| |
---|---|---|
SolutionIn order to get user input from JOptionPane you will need to import the swing package. You can import swing package by import javax.swing.*; or import javax.swing.JOptionPane; The next step is declaring variables. In this program we will need three inegert variables. One variable will be used as <math>F(n)</math> and another variable will be used as <math>F(n-1)</math>. Our initial value for <math>F(n)</math> will be 1 and <math>F(n-1)</math> will be 0, referring to the fibonacci formula. int n = 1 int n-1 = 0 String str = get the user input using JOptionPane int input = cast str to integer Once you have all the variables ready, make a for loop that starts from 0 and ends when the counter is less than n (user input). In this for loop, the increment will be by 1. for(count = 0; count < user input; count increment by 1) { } We will need another variable to keep track of <math>F(n-2)</math>. You simply set it <math>F(n-2) = F(n-1)</math> as it starts a new iteration, and set <math>F(n-1) = F(n)</math>. Then sum up the <math>F(n-2) + F(n-1)</math> which will now be <math>F(n)</math>. (n-2) = (n-1) (n-1) = n n = (n-2) + (n-1) When the for loop ends, you will have the nth fibonacci number. At last, print it out using JOptionPane. When you are printing the result out with JOptionPane, note that you have to put 'null' before the actual message you want to print out. See below for the solution code. | ||
Code |