Difference between revisions of "Fibonacci sequence"
From CompSciWiki
Line 4: | Line 4: | ||
|Problem=Write a complete Java program Fibonacci that:<br/> | |Problem=Write a complete Java program Fibonacci that:<br/> | ||
− | *prompts the | + | *prompts the user to enter an input n and cast it to integer<br/> |
− | *prints out the | + | *get the nth fibonacci numbers using ''FOR'' loop, based on the user's input<br/> |
+ | *prints out the nth fibonacci number, using JOptionPane.showMessageDialog method.<br/> | ||
<br/> | <br/> | ||
+ | Note that the fibonacci sequence is F(0) = 1, F(1) = 1, F(n) = F(n-1) + F(n-2) for n > 1.<br/> | ||
+ | Example: F(5) = 1, 1, 2, 3, 5<br/> | ||
Your output should look something like<br/> | Your output should look something like<br/> | ||
− | "The | + | "The 5th number of the fibonacci sequence is 5."<br/> |
<br/> | <br/> | ||
− | + | NOTE: You may assume that the user always enters correct intenger format. | |
− | + | ||
|SolutionCode=public class Fibonacci | |SolutionCode=public class Fibonacci |
Revision as of 22:53, 1 April 2010
Back to the Program-A-Day homepage
ProblemWrite a complete Java program Fibonacci that:
|
...by students
An image or By Students section | |
---|---|---|
Solutionpublic class Fibonacci { public static void main(String[] args) { int answer = 0; for(int n = 1; n < 11; n++) { if(n == 0 | ||
Code |