Here is the code take a look at it and tell me what you think.
This is the Dialog coding for input and output.
import javax.swing.JOptionPane;
public class FibonacciJDialog
{
public static void main(String[] args)
{
String num;
int n;
num =
JOptionPane.showInputDialog("Enter a number: ");
n = Integer.parseInt(num);
Fibonacci box = new Fibonacci();
JOptionPane.showMessageDialog(null,"That Fibonacci Number is " + box.Fib(n));
}
}
This is the Fibonacci.java code
public class Fibonacci
{
int Fib (int n)
{
int in1=1,in2=1;
int sum=0;//initial value
int index=1;
while (index<n)
{
sum = in1 + in2;
in1 = in2;
in2 = sum;
index++;
// sum=the sum of 2 values;
// in1 gets in2
// in2 gets sum
// increment index
}
return sum;
}
}

New Topic/Question
Reply



MultiQuote



|