Difference between revisions of "Fibonacci sequence"

From CompSciWiki
Jump to: navigation, search
(Fixed typos and formatting problems)
(Removed breaks.)
Line 13: Line 13:
 
"The 5th number of the fibonacci sequence is 5."<br>
 
"The 5th number of the fibonacci sequence is 5."<br>
 
<br>
 
<br>
You may assume that the user always enters correct integer format.<br>
+
You may assume that the user always enters correct integer format.
  
 
|SideSectionTitle=Mid-term Review
 
|SideSectionTitle=Mid-term Review
Line 46: Line 46:
  
 
|Solution=
 
|Solution=
In order to get user input from JOptionPane, you will need to import the swing package.<br>
+
In order to get user input from JOptionPane, you will need to import the swing package.
You can import swing package by <br>
+
You can import swing package by
<br>
+
 
 
<pre>
 
<pre>
 
import javax.swing.*;  
 
import javax.swing.*;  
Line 56: Line 56:
 
import javax.swing.JOptionPane;
 
import javax.swing.JOptionPane;
 
</pre>
 
</pre>
<br>
+
 
The next step is declaring variables. In this program we will need three inegert variables.<br>
+
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>(F(0) = 1, F(1) = 1)</br>
One variable will be used as F(n) and another variable will be used as F(n-1). <br>
+
Once you declare the necessary variables, the last variable will be the casted integer from the user input.
Our initial value for F(n) will be 1 and F(n-1) will be 0, referring to the fibonacci formula. (F(0) = 1, F(1) = 1)<br>
+
 
Once you declare the necessary variables, the last variable will be the casted integer from the user input.<br>
+
<br>
+
 
<pre>
 
<pre>
 
int n = 1
 
int n = 1
Line 68: Line 66:
 
int input = cast str to integer
 
int input = cast str to integer
 
</pre>
 
</pre>
<br>
+
 
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).<br>
+
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.
In this for loop, the increment will be by 1.<br>
+
 
<br>
+
 
<pre>
 
<pre>
 
for(count = 0; count < user input; count increment by 1)
 
for(count = 0; count < user input; count increment by 1)
Line 77: Line 74:
 
}
 
}
 
</pre>
 
</pre>
<br>
+
 
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).<br>
+
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).
Then sum up the F(n-2) + F(n-1) which will now be F(n).<br>
+
 
<br>
+
 
<pre>
 
<pre>
 
(n-2) = (n-1)
 
(n-2) = (n-1)
Line 86: Line 82:
 
n = (n-2) + (n-1)
 
n = (n-2) + (n-1)
 
</pre>
 
</pre>
<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. 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.  
At last, print it out using JOptionPane.<br/>
+
 
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>
+
<br>See below for the solution code.<br>
<br>
+
See below for the solution code.<br>
+
  
 
}}
 
}}

Revision as of 19:53, 8 April 2010

Back to the Program-A-Day homepage

Problem

Write a complete Java program titled Fibonacci that:

  • prompts the user to enter an input number and casts it to integer
  • gets the nth fibonacci number using a for loop, based on the user's input
  • prints out the nth fibonacci number, using JOptionPane Methods.


Note that the fibonacci sequence is F(0) = 1, F(1) = 1, F(n) = F(n-1) + F(n-2) for n > 1.
Example: F(5) = 1, 1, 2, 3, 5, 8
Your output should look something like
"The 5th number of the fibonacci sequence is 5."

You may assume that the user always enters correct integer format.

 

Mid-term Review

Wiki trays01.jpg


Solution

In 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 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.
(F(0) = 1, F(1) = 1)</br> Once you declare the necessary variables, the last variable will be the casted integer from the user input.

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 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).

(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

Solution Code

Back to the Program-A-Day homepage