Difference between revisions of "Fibonacci sequence"

From CompSciWiki
Jump to: navigation, search
Line 15: Line 15:
 
You may assume that the user always enters correct intenger format.<br/>
 
You may assume that the user always enters correct intenger format.<br/>
  
|SolutionCode
+
|SolutionCode=
{import javax.swing.JOptionPane;<br/>
+
import javax.swing.JOptionPane;<br/>
 
<br/>
 
<br/>
 
public class Fibonacci <br/>
 
public class Fibonacci <br/>
Line 36: Line 36:
 
  System.out.println("The " + n + "th Fibonacci number is " + current);<br/>
 
  System.out.println("The " + n + "th Fibonacci number is " + current);<br/>
 
     }<br/>
 
     }<br/>
}
+
 
  
 
|SideSectionTitle=...by students
 
|SideSectionTitle=...by students

Revision as of 23:20, 1 April 2010

Back to the Program-A-Day homepage

Problem

Write a complete Java program Fibonacci that:

  • prompts the user to enter an input n and cast it to integer
  • get the nth fibonacci numbers using FOR loop, based on the user's input
  • prints out the nth fibonacci number, using JOptionPane.showMessageDialog method.


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

 

...by students

float
Taken from http://www.flickr.com/photos/daniello/565304023/

An image or By Students section

Solution

Code

Solution Code

Back to the Program-A-Day homepage