## clairvoyance

Hello,
I have a to make a class called fibonacci. This is what is given to us :
fibonacci(0)=1
fibonacci(1)=1
fibonacci(n)= fibonacci(n-­1)+fibonacci(n–2)

Below is an example of running the fibonacci class as an input of 6. The output is:
fibo(0)=1
fibo(1)=1
fibo(2)=2
fibo(3)=3
fibo(4)=5
fibo(5)=8
fibo(6)=13

My code is:

``````import java.util.*;

public class Fibonacci {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the size of the fibonacci series: ");
int num = s.nextInt();
int a=0,b=0,c=1;
for(int i=1;i<=num;i++){
System.out.println(c);
a=b;
b=c;
c=a+b;
}
}
}``````

My output is:
Enter the size of the fibonacci series:
6
1
1
2
3
5
8

Why is my answer not matching his? Is there something that I am doing wrong?

## sathya_s

try this program:
------------------

``````import java.io.*;
class fib
{
public static void main(String args[]) throws IOException
{

DataInputStream in=new DataInputStream(System.in);
String str;
int i,n;
int f1=-1,f2=1,f3;
System.out.println( "enter the number:");
n=Integer.parseInt(str);
System.out.println( "the fibonacci series is:");
for( i=1;i<=n;i++)
{
f3=f1+f2;
System.out.println(f3);
f1=f2;
f2=f3;
}

}}``````

-----------
the output is:
C:\jdk1.3\bin>javac fib.java
Note: fib.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
C:\jdk1.3\bin>java fib
enter the number:
8
the fibonacci series is:
0
1
1
2
3
5
8
13
C:\jdk1.3\bin>

## JamesCherrill 3,661

Are you really using JDK 1.3? That was released in 2000 replaced in 2002. The language had major upgrades with 1.5, and we are currently on 1.6
Time to update.
ps If you really want to give sample code to someone trying to learn the language, please don't use deprecated APIs.

## sathya_s

ya i have jdk1.6 too..thanks for giving me such suggestion

## JamesCherrill 3,661

OK. It was the path to your jdk C:\jdk1.3\bin that rang alarm bells!

## clairvoyance

Thanks guys, I was able to solve the problem.

## Eric Cute 33

Also ifyou want, you can post the solution to your problem so that other users needing the same help can read your post (",)

## clairvoyance

Here is the final code that I came up with and got it working

``````import java.util.*;

public class Fibonacci {
public static void main(String[] args) {

Scanner s = new Scanner(System.in);
System.out.println("Enter the size of the fibonacci series: ");

int num = s.nextInt();
int a=1,b=1,c=1;

for(int i=1;i<=num;i++){

System.out.println("fibo(" + i + ") = " + (c));
a=b;
b=c;
c=a+b;

}
}

}``````

P.S. I just changed a=1 and b=1
Hope this is helpful to someone.
Thanks.

## -ordi- 6

Dynamic programming is good to solve that problem (better than recursive).

``````table[0] = 1
table[1] = 2

table[i] = table[i - 2] + table[i - 1]``````