How do I calculate the fibonacci value of a number using while statement?

on
1. prompt the user to imput a number
2. if the number is less than 0, print Invalid number and quit the application
3. if the number if larger than or equal 0, then use fibonacci equation to calculate its fibonacci value
4. the result should be in format Fib(4) = 3

this is what I have but, I dont know if it is right
public static void main(String [] args){

System.out.println("enter any number");

public static int fibonacci(int n){

if(n==0)
return 0;
else if (n==1)
return 1;

else {

for (int i=0;i<=n;i++){
int fn = 0;
int f0=0;
int f1=1;

int f2 = f0+f1;
int f3 = f1+f2;
int f4 = f2+f3;
int f5=f3+f4;
int f6=f5+f4;
int f7=f6+f5;
int f8=f7+f6;
int f9=f8+f7;
int f10=f9+f8;
return (fn-1) + (fn-2);

}
}
return n;

}
}
thank you
Comment
Watch Question

Do more with

EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Expert of the Quarter 2010
Expert of the Year 2010
Commented:
The while statement goes like this:

public static int fibonacci(int n) {
int fib = 0;  // set the value to 0 to start
int loopvar = 1; // the loop variable
int lastvalue = 0; // the last value
int tmp; // used to swap the last value
// while the loop variable is less than n
//     transfer fib value into the variable tmp
//     set the fib value to fib + lastvalue, i.e. the accumulation of last 2 values in the sequence
//     transfer tmp into the variable lastvalue
return fib;

--------
so the variables progress like this through the loop
start: fib, lastvalue
0, 0, 0
1, 1, 0  << fib 0 moves into lastvalue
2, 1, 1  << fib 1 moves into lastvalue
3, 2, 1  << fib 1 moves into lastvalue, fib = 1+1 from previous fib+lastvalue
4, 3, 2
etc
Java Developer
Top Expert 2010
Commented:
have a look at this and let me know if you have any questions

public static void main(String[] args) throws IOException {

System.out.println("enter any number");

System.out.println(fibonacci(n));
}

public static int fibonacci(int n) {

if (n == 0)
return 0;
else if (n == 1)
return 1;

else {
int[] fibs = new int[2];
fibs[0] = 0;   // fib of n-2
fibs[1] = 1;   // fib of n-1

int i = 1;
int fib = 0;   // start at 0
while (n>i) {       // loop till we get to target number
fib = fibs[0] + fibs[1];   // calculate next fib number
fibs[0] = fibs[1];         // shift n-2 and n-1
fibs[1] = fib;
i++;
}
return fib;
}
}
Commented:
Simpler fibonnacci method :

public static int fibonnacci(int n) {
int result = n;

if (n > 1) {
int last = 0;
int cur = 1;
while (n-- > 1) {
cur = cur + last;
last = cur - last;
}
result = last;
}

return result;
}

Commented:

``````public int calcfibo(int i){
cal = i;
int test = 1;
int j=i;
while((j%2 != 0 && i>0)||(j%2 == 0 && i>1)){
swap = first+last;
result = swap;
test = i%2;
if(test==0){
first = swap;
}else{
last = swap;
}
i--;
}
return result;
}
``````

Commented:
jzkidding: I don't think your solution can work :

No declaration for cal. Then it's not used.
No declaration, no initialisation for first and last.

You turn a simple question into a complex solution.

Commented:
apologize greisch , i have just given the signature of the calculating logic. below code has the complete program
``````package fibo;

import java.io.IOException;

public class Fibonacci {

int first = 0;
int last = 1;
int swap;
int result;

public int calcfibo(int i){
int test = 1;
int j=i;
while((j%2 != 0 && i>0)||(j%2 == 0 && i>1)){
swap = first+last;
result = swap;
test = i%2;
if(test==0){
first = swap;
}else{
last = swap;
}
i--;
}
return result;
}

public int getInput() throws IOException{
System.out.println("Enter a number for Fibonacci Series");
System.out.println("Result \t" + (fiboNum));
return (Integer.parseInt(fiboNum));
}
public static void main(String[] args) {
Fibonacci fibo = new Fibonacci();
fibo.calcfibo(4);
}
}
``````

Commented:
jzkidding:

Your complete program won't work very well.

For example, if you call fibo.calcfibo(4) twice in main, the result of the second call will be false.

Commented:
yes greisch you are right, even i tried it. IT is because i have initialized the variables at class level and those values are changed during the first call to calFibo method.

This can be overcome by initializing the variables at the method level.It works i tested for same as said by greisch.

I welcome other comments too. I attach the updated code
``````package fibo;

import java.io.IOException;

public class Fibonacci {

int first ;
int last ;
int swap;
int result;

public int calcfibo(int i){
first = 0;
last = 1;
int test = 1;
int j=i;
while((j%2 != 0 && i>0)||(j%2 == 0 && i>1)){
swap = first+last;
result = swap;
test = i%2;
if(test==0){
first = swap;
}else{
last = swap;
}
i--;
}
return result;
}

public int getInput() throws IOException{
System.out.println("Enter a number for Fibonacci Series");
//		System.out.println("Result \t" + (fiboNum));
return (calcfibo(Integer.parseInt(fiboNum)));
}
public static void main(String[] args) {
Fibonacci fibo = new Fibonacci();
try {
System.out.println("Result \t"+fibo.getInput());
System.out.println("Result \t"+fibo.getInput());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
``````

Commented:
A good programming practice is to keep variables as local as possible.
Here you create 4 fields in Fibonacci to maintain values used by the calcfibo method only. It is not so good.
You should turn these fields to local variables in calcfibo.

Another good programming practice is to keep things as simple as possible (http://www.extremeprogramming.org/rules/simple.html). Here your solution is over complicated. If I see this code without knowing it calculates the Fibonacci suite, it will be difficult for me to figure it out.

public static int fibonnacci(int n) {
int result = n;

if (n > 1) {
int last = 0;
int cur = 1;
while (n-- > 1) {
cur = cur + last;
last = cur - last;
}
result = last;
}

return result;
}

Here the test in the while loop is far most comprehensible and the code in the loop too. A side effect is that this code will run faster and will be easer to debug.

Commented:
greisch i tried your snippet too. even that is not yelding the desired result

Commented:
greisch i tried your snippet too. even that is not yielding the desired result. I tried as n=4 it gives the result as 2. F(4)=3

Commented:
"while (n-- >= 1)" or "while (n-- > 0)"  in place of "while (n-- > 1)" works beter.
Chief Technology Officer
Most Valuable Expert 2011

Commented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:
Split: http:#33955719, http:#33953177, http:#33953246

If you feel this question should be closed differently, post an objection and a moderator will read all objections and then close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

mwvisa1
Experts-Exchange Cleanup Volunteer

Do more with