# A problem with my Fibonacci Sequence code

Page 1 of 1

## 8 Replies - 685 Views - Last Post: 20 March 2014 - 03:18 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=342685&amp;s=a7eac01d62d4e63af48088da9bc5db63&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 LooneyTunerIan

Reputation: 0
• Posts: 5
• Joined: 19-March 14

# A problem with my Fibonacci Sequence code

Posted 19 March 2014 - 05:08 PM

I got a problem with my Fibonacci Sequence Code:

```public class FibSeqByIanNeumann {

public static void main(String[] args)
{

Scanner input = new Scanner(System.in);
int fib, count;

count = input.nextInt();

fib = 0;

for (int i=2; i<=count; i++)
{

fib = count + fib;

}

System.out.print("When a Fibonacci Sequence runs " + count + " times, it goes like this: " + "\n");
System.out.println(fib + ", ");

}
}
```

It's not running the way it's supposed to. Can anyone help fix it?

Is This A Good Question/Topic? 0

## Replies To: A problem with my Fibonacci Sequence code

### #2 modi123_1

• Suitor #2

Reputation: 13868
• Posts: 55,364
• Joined: 12-June 08

## Re: A problem with my Fibonacci Sequence code

Posted 19 March 2014 - 05:10 PM

The last time I checked the Fibonacci Sequence was the previous number plus the current number where the 0 and 1 indexed terms are 0 and 1.

Your for loop does not appear to be doing that..

### #3 LooneyTunerIan

Reputation: 0
• Posts: 5
• Joined: 19-March 14

## Re: A problem with my Fibonacci Sequence code

Posted 19 March 2014 - 05:11 PM

Can anyone show me it's supposed to work?

### #4 modi123_1

• Suitor #2

Reputation: 13868
• Posts: 55,364
• Joined: 12-June 08

## Re: A problem with my Fibonacci Sequence code

Posted 19 March 2014 - 05:18 PM

I would suggest starting with a few variables..

Have three variables..

temp, current, previous.

Copy current to temp.
Set current to be current plus previous.
Set previous to temp.
Repeat.

### #5 LooneyTunerIan

Reputation: 0
• Posts: 5
• Joined: 19-March 14

## Re: A problem with my Fibonacci Sequence code

Posted 19 March 2014 - 05:18 PM

I'm confused.

### #6 modi123_1

• Suitor #2

Reputation: 13868
• Posts: 55,364
• Joined: 12-June 08

## Re: A problem with my Fibonacci Sequence code

Posted 19 March 2014 - 05:21 PM

I literally wrote it out for you. Outside of just giving your the code - how about you try matching the code to what I typed out.

### #7 ben255

Reputation: 39
• Posts: 504
• Joined: 09-September 13

## Re: A problem with my Fibonacci Sequence code

Posted 20 March 2014 - 11:56 AM

i belive ive seen code for this in this forum from the past, try doing a search

### #8 LooneyTunerIan

Reputation: 0
• Posts: 5
• Joined: 19-March 14

## Re: A problem with my Fibonacci Sequence code

Posted 20 March 2014 - 03:09 PM

All I need is a java code that only involve an easy for loop and can easily start at zero.

So far, none of the scripts I've looked up contains those things.

### #9 modi123_1

• Suitor #2

Reputation: 13868
• Posts: 55,364
• Joined: 12-June 08

## Re: A problem with my Fibonacci Sequence code

Posted 20 March 2014 - 03:18 PM

Again - I wrote out the sequence of steps.. you should be able to at least try to put code to it.