Difference between revisions of "Fibonacci sequence"
Line 11: | Line 11: | ||
Example: <math>F(5) = 1, 1, 2, 3, 5, 8</math><br> | Example: <math>F(5) = 1, 1, 2, 3, 5, 8</math><br> | ||
Your output should look something like<br> | Your output should look something like<br> | ||
− | "The | + | "The 5<sup>th<sup> number of the fibonacci sequence is 5."<br> |
<br> | <br> | ||
You may assume that the user always enters correct integer format. | You may assume that the user always enters correct integer format. | ||
Line 64: | Line 64: | ||
<br> | <br> | ||
The next step is declaring variables. <br> | The next step is declaring variables. <br> | ||
− | In this program we will need three | + | In this program we will need three integer variables. <br> |
One variable will be used as <math>F(n)</math> and another variable will be used as <math>F(n-1)</math>. <br> | One variable will be used as <math>F(n)</math> and another variable will be used as <math>F(n-1)</math>. <br> | ||
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> | 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> | ||
Line 97: | Line 97: | ||
<br> | <br> | ||
When the for loop ends, you will have the n<sup>th</sup> fibonacci number. <br> | When the for loop ends, you will have the n<sup>th</sup> fibonacci number. <br> | ||
− | At last, print it out using <i>JOptionPane</i>. | + | At last, print it out using <b><i>JOptionPane</b></i>. |
− | 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. <br> | + | When you are printing the result out with <b><i>JOptionPane</b></i>, note that you have to put 'null' before the actual message you want to print out. <br> |
See below for the solution code.<br> | See below for the solution code.<br> | ||
}} | }} |
Revision as of 22:34, 10 April 2010
Back to the Program-A-Day homepage
ProblemWrite a complete Java program titled Fibonacci that:
|
By students..Before taking Comp 1010 course, I had never had any programming experiences. I did not know much, and I have had many problems with assignments. I'd like to give you some tips that helped me with doing programming assignments. | |
---|---|---|
SolutionIn order to get user input from JOptionPane you will need to import the swing package. import javax.swing.*; or import javax.swing.JOptionPane;
int n = 1 int n-1 = 0 String str = get the user input using JOptionPane int input = cast str to integer
for(count = 0; count < user input; count increment by 1) { }
| ||
Code |