Difference between revisions of "Fibonacci sequence"
From CompSciWiki
Line 28: | Line 28: | ||
An image or By Students section | An image or By Students section | ||
− | |Solution=public class Fibonacci | + | |Solution=import javax.swing.JOptionPane;<br/> |
− | { | + | <br/> |
− | + | public class Fibonacci <br/> | |
− | + | {<br/> | |
− | + | public static void main(String[] args)<br/> | |
− | + | { <br/> | |
− | + | int current = 1;<br/> | |
− | + | int previous = 0;<br/> | |
− | + | <br/> | |
− | + | int n = Integer.parseInt(JOptionPane.showInputDialog("Please enter n"));<br/> | |
− | + | <br/> | |
− | + | for(int i = 0; i < n; i++)<br/> | |
− | + | {<br/> | |
− | + | int temp = previous;<br/> | |
− | + | previous = current;<br/> | |
− | + | current = temp + previous;<br/> | |
− | + | }<br/> | |
+ | |||
+ | System.out.println("The " + n + "th Fibonacci number is " + current);<br/> | ||
+ | }<br/> | ||
}} | }} |
Revision as of 23:00, 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 | |
---|---|---|
Solutionimport javax.swing.JOptionPane; public static void main(String[] args) | ||
Code |