Difference between revisions of "Fibonacci sequence"

From CompSciWiki
Jump to: navigation, search
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 5th number of the fibonacci sequence is 5."<br>
+
"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 ineger 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 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

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 <math>F(0) = 1, F(1) = 1, F(n) = F(n-1) + F(n-2) for n > 1</math>.
Example: <math>F(5) = 1, 1, 2, 3, 5, 8</math>
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.

 

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.

First, always write comments. This may depend on each student's skill, however, you won't finish your assignments or writing codes in few hours. Then, when you work on it later you are very likely to forget what you were doing. Writing comments will help you remember what you were doing, and it will be easier for you to fix errors by going through each of the code.
Second, take a break if you are stuck. If you are mentally tired and cannot figure a way out, take a break. Otherwise, you will even mess up with the parts that were actually correct. This will make you extremely stressed out, and you will most likely NOT finish your assignment.
Third, do not look up solutions on internet. If you are in first year and barely know few things, you will never understand solutions on internet. There are tons of people who write programs using different techniques than our university teaches. You will simply waste your time on looking for solutions.
Fourth, start assignments ahead of time. I used to start assignments a day before, and I had many troubles finishing them properly. Even if the first year computer science assignments are easy, they still take up some time. Besides, if you start taking second year computer science courses, you will NEVER finish an programming assignments in one day.

These tips always helped me doing assignments. However, these are very general tips for doing assignments. As you experience more and more in programming, you will find out more tips that are more specific to your case.

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 integer 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.
<math>F(0) = 1, F(1) = 1</math>

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

<math>(n-2) = (n-1)</math>

<math>(n-1) = n</math>

<math>n = (n-2) + (n-1)</math>

When the for loop ends, you will have the nth fibonacci number.
At last, print it out using JOptionPane</b>. When you are printing the result out with <b>JOptionPane</b>, 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