# return fibonacci number using array

Page 1 of 1

## 3 Replies - 15126 Views - Last Post: 18 January 2010 - 12:48 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=150544&amp;s=032a60a66aae117ddc67a85ed5313105&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Nouf ~

Reputation: 0
• Posts: 9
• Joined: 18-December 09

# return fibonacci number using array

Posted 18 January 2010 - 11:12 AM

Hi

Question :

Write a program that reads an integer n, then declares an array of n elements. The program then calls a method that fills the array with the first n Fibonacci numbers. Then main reads a list of numbers less than n that denote the position of a number and calls another method that uses the array to return the Fibonacci number in that position. Main stops when a negative number is read.

my code :

```import java.util.*;
public class FibonacciNumbers
{
static Scanner console=new Scanner (System.in);
static int i,pos,f,n;
static int [] fibo;

public static void main (String[]args)
{

fibo=new int [n];

System.out.println("Enter the length");
n=console.nextInt();

fill(fibo);

System.out.println(" Enter an integers less than the length  then enter -1 when finished");
pos=console.nextInt();
while( pos<n && pos!=-1)
{
System.out.print(" The fibonacci number at this position is : ");
position(pos);
pos=console.nextInt();
}

}

public static void fill(int[]fibo)
{

for (i=0;i<fibo.length;i++)
fibo[i]=fibo[i-1]+fibo[i-2];

}

public static int position(int pos)
{

return fibo[pos];

}

```

what's rong with it pleeeease !!

Is This A Good Question/Topic? 0

## Replies To: return fibonacci number using array

### #2 EdwinNameless

Reputation: 123
• Posts: 720
• Joined: 15-October 09

## Re: return fibonacci number using array

Posted 18 January 2010 - 11:34 AM

Nouf ~, on 18 Jan, 2010 - 05:12 PM, said:

what's rong with it pleeeease !!

Several things. Most notably:

```fibo=new int [n];

System.out.println("Enter the length");
n=console.nextInt();

```

You must define fibo after having read n on ths console, otherwise its size will always be 0.

```	public static void fill(int[] fibo) {

for (i = 0; i < fibo.length; i++)
fibo[i] = fibo[i - 1] + fibo[i - 2];

}

```

This doesn't cater for the 2 first values of the Fibonacci series... So how do you set fibo[0] and fibo[1]?

### #3 Gasper

Reputation: 13
• Posts: 66
• Joined: 29-May 09

## Re: return fibonacci number using array

Posted 18 January 2010 - 11:43 AM

...and printing results.
So it should look something like
```import java.util.*;
public class FibonacciNumbers
{
static Scanner console=new Scanner (System.in);
static int i,pos,f,n;
static int [] fibo;

public static void main (String[]args)
{
System.out.println("Enter the length");
n=console.nextInt();
fibo=new int [n];

fill(fibo);

System.out.println(" Enter an integers less than the length  then enter -1 when finished");
pos=console.nextInt();
while( pos<n && pos!=-1)
{
System.out.print(" The fibonacci number at this position is : ");
System.out.println(position(pos));
pos=console.nextInt();
}
}

public static void fill(int[]fibo)
{
fibo[0] = 1;
fibo[1] = 1;

for (i=2;i<fibo.length;i++)
fibo[i]=fibo[i-1]+fibo[i-2];
}

public static int position(int pos)
{
return fibo[pos];
}

}

```

### #4 Nouf ~

Reputation: 0
• Posts: 9
• Joined: 18-December 09

## Re: return fibonacci number using array

Posted 18 January 2010 - 12:48 PM

many Thanks