Difference between revisions of "Fibonacci sequence"
m |
m |
||
Line 66: | Line 66: | ||
In this program we will need three integer variables. <br> | 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 | + | 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> |
<b><math>F(0) = 1, F(1) = 1</math></b><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.<br> | Once you declare the necessary variables, the last variable will be the casted integer from the user input.<br> | ||
Line 96: | Line 96: | ||
<math>n = (n-2) + (n-1)</math><br> | <math>n = (n-2) + (n-1)</math><br> | ||
<br> | <br> | ||
− | When the for loop ends, you will have the n<sup>th</sup> | + | When the for loop ends, you will have the n<sup>th</sup> fibonacci number. <br> |
At last, print it out using <b>''JOptionPane''</b>.<br> | At last, print it out using <b>''JOptionPane''</b>.<br> | ||
When you are printing the result out with JOptionPane, note that you have to put <b>'null'</b> before the actual message you want to print out. <br> | When you are printing the result out with JOptionPane, note that you have to put <b>'null'</b> before the actual message you want to print out. <br> |
Revision as of 22:40, 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 |