Difference between revisions of "Fibonacci sequence"
Line 47: | Line 47: | ||
}</pre> | }</pre> | ||
− | |Solution= | + | |Solution= |
− | In order to get the user input by JOptionPane, you will first have to import the swing package. | + | In order to get the user input by JOptionPane, you will first have to import the swing package.<br/> |
− | You can import swing package by | + | You can import swing package by <br/> |
− | + | <br/> | |
+ | <pre> | ||
import javax.swing.*; or import javax.swing.JOptionPane; | import javax.swing.*; or import javax.swing.JOptionPane; | ||
− | + | </pre> | |
− | Then, the next step is declaring variables; in this program we will need three int variables. | + | <br/> |
− | One variable will be used as F(n) and another variable will be used as F(n-1). | + | Then, the next step is declaring variables; in this program we will need three int variables.<br/> |
− | 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) | + | 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> | ||
int n = 1 | int n = 1 | ||
int n-1 = 0 | int n-1 = 0 | ||
String str = get the user input using JOptionPane | String str = get the user input using JOptionPane | ||
int input = cast str to integer | int input = cast str to integer | ||
− | + | </pre> | |
− | 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/> |
− | In this for loop, the increment will be by 1. | + | 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/> |
− | + | In this for loop, the increment will be by 1.<br/> | |
+ | <br/> | ||
+ | <pre> | ||
for(count = 0; count < user input; count increment by 1) | for(count = 0; count < user input; count increment by 1) | ||
{ | { | ||
} | } | ||
− | + | </pre> | |
− | 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/> |
− | Then sum up the F(n-2) + F(n-1) which will now be F(n). | + | 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/> |
− | + | Then sum up the F(n-2) + F(n-1) which will now be F(n).<br/> | |
+ | <br/> | ||
+ | <pre> | ||
(n-2) = (n-1) | (n-2) = (n-1) | ||
(n-1) = n | (n-1) = n | ||
n = (n-2) + (n-1) | n = (n-2) + (n-1) | ||
+ | </pre> | ||
+ | <br/> | ||
+ | When the for loop ends, you will have exactly nth fibonacci number you wanted.<br/> | ||
+ | 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 meesage you want to print out. <br/> | ||
+ | <br/> | ||
+ | See below for the code.<br/> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
}} | }} |
Revision as of 15:36, 5 April 2010
Back to the Program-A-Day homepage
ProblemWrite a complete Java program Fibonacci that:
|
...by students
An image or By Students section | |
---|---|---|
SolutionIn order to get the user input by JOptionPane, you will first have to import the swing package. import javax.swing.*; or import javax.swing.JOptionPane;
int n = 1 int n-1 = 0 String str = get the user input using JOptionPane int input = cast str to integer
for(count = 0; count < user input; count increment by 1) { }
(n-2) = (n-1) (n-1) = n n = (n-2) + (n-1)
| ||
Code |