# Fibonacci - Iterative & Recursive

Page 1 of 1

## 5 Replies - 615 Views - Last Post: 29 June 2013 - 04:27 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=323530&amp;s=dd16c39311cccaf5e0fe2369b5a1d693&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jeffries7

Reputation: 0
• Posts: 2
• Joined: 13-November 12

# Fibonacci - Iterative & Recursive

Posted 24 June 2013 - 08:41 AM

I need to write a program that takes a users input and prints the fibonacci sequence then prints the sum of them.

It is printing the sequence fine but when it comes to the sum it's off. Any sort of help would be great. Not expecting anyone to give me answers or do it for me, just a little help />

If you require any more details/info let me know.

n is the users input

```#include <iostream>
using namespace std;

int fib(int n)
{
if (n == 0)
return 0;
if (n == 1)
return 1;

return (fib(n-1)+fib(n-2));
}

int fib2(int n)
{
int a = 0, b = 1, c = 0;
for (int i = 0; i < n; i++)
{
cout << a << " ";
c = a + b;
a = b;
b = c;
}

return b;
}
```

Is This A Good Question/Topic? 0

## Replies To: Fibonacci - Iterative & Recursive

### #2 jon.kiparsky

• Pancakes!

Reputation: 9436
• Posts: 16,359
• Joined: 19-March 11

## Re: Fibonacci - Iterative & Recursive

Posted 24 June 2013 - 09:00 AM

Debugging 101: Find the spot where the error becomes manifest. Figure out which variable is wrong, and track back until you find the spot where you set it incorrectly.
Ask yourself: "Which value is wrong, and how did it get that way?" Repeat as needed.

Questions 101: If you have a problem, describe it. Particularly, if you have a wrong value, tell us what it is!

This post has been edited by jon.kiparsky: 24 June 2013 - 09:14 AM

### #3 jjl

• Engineer

Reputation: 1168
• Posts: 4,782
• Joined: 09-June 09

## Re: Fibonacci - Iterative & Recursive

Posted 24 June 2013 - 01:54 PM

### #4 jeffries7

Reputation: 0
• Posts: 2
• Joined: 13-November 12

## Re: Fibonacci - Iterative & Recursive

Posted 25 June 2013 - 03:45 AM

Thank you Jon I will continue to look into it.

jjl - I did not post it because it only contains the GUI, therefore not needed here

### #5 jimblumberg

Reputation: 4887
• Posts: 15,264
• Joined: 25-December 09

## Re: Fibonacci - Iterative & Recursive

Posted 25 June 2013 - 04:16 AM

Quote

It is printing the sequence fine but when it comes to the sum it's off.

You need to post the code that calls these functions and sums the values.

Also show a sample of your input, the output of the program, and what you expect the output to be based on that input.

Jim

### #6 buffalobill

Reputation: 21
• Posts: 188
• Joined: 08-July 08

## Re: Fibonacci - Iterative & Recursive

Posted 29 June 2013 - 04:27 PM

The two functions seem to have the same objective namely to print the Fibonacci series. The fib () function seems OK but fib2() has errors if its to be used for this purpose. Are you saying that fib2 () is meant to be summing the series? If so line 21 needs to go below line 23 and a further statement like sum+=c;needs to be added.
Hope this helps as I have not tested this suggestion.