# Fibonacci.h recursive method

Page 1 of 1

## 3 Replies - 1995 Views - Last Post: 23 July 2008 - 11:12 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=58470&amp;s=522673ff9928a86ce653fe56bf1103b4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mapes479

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 17-February 08

# Fibonacci.h recursive method

Posted 23 July 2008 - 07:39 PM

I'm having problems finishing up my Fibonacci.h program. The assignment is to do the following:

Write a header file which that you will call Fibonacci.h. The file contains a recursive method Fib with one parameter of type long and returns the Fibonacci value. Remember to use long as a data type for the returned fibonacci result by the method Fib because the number could be very large.

Here's the coding I have so far. I am new to C++ and can't tell if I'm headed in the right direction or what I'm doing wrong. Could someone please help me with this? I have my .cpp file completed already.

```int fibonacci(int num, int num1, int num2)

long fib(unsigned long n)
{
if (n <= 1) {
return n;
} else {
return fib(n-1)+fib(n-2);
}
}

int fibonacci(int num, int num1, int num2)
{
int ctr=2,fibo=0;
while(ctr<num)
{
fibo=num1+num2;
num1=num2;
num2=fibo;ctr++;
}
return fibo;
}
```

Is This A Good Question/Topic? 0

## Replies To: Fibonacci.h recursive method

### #2 penguin2

• D.I.C Head

Reputation: 3
• Posts: 90
• Joined: 22-July 08

## Re: Fibonacci.h recursive method

Posted 23 July 2008 - 07:52 PM

I don't believe that you are allowed to say "long fib(....". There is a long int, but no loose long.

This:
int fibonacci(int num, int num1, int num2)
needs a semicolon.
Was This Post Helpful? 0

### #3 mapes479

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 17-February 08

## Re: Fibonacci.h recursive method

Posted 23 July 2008 - 08:12 PM

penguin2, on 23 Jul, 2008 - 07:52 PM, said:

I don't believe that you are allowed to say "long fib(....". There is a long int, but no loose long.

This:
int fibonacci(int num, int num1, int num2)
needs a semicolon.

Here is my revised version: I'm getting the following error - how do I fix this?

error C2447: '{' : missing function header (old-style formal list?)

```#include <iostream>

using namespace std;

int fibonacci(int num, int num1, int num2);

{
if (n <= 1) {
return n;
} else {
return fib(n-1)+fib(n-2);
}
}

int fibonacci(int num, int num1, int num2)
{
int ctr=2,fibo=0;
while(ctr<num)
{
fibo=num1+num2;
num1=num2;
num2=fibo;ctr++;
}
return fibo;
}
```

Was This Post Helpful? 0

### #4 born2c0de

• printf("I'm a %XR",195936478);

Reputation: 186
• Posts: 4,673
• Joined: 26-November 04

## Re: Fibonacci.h recursive method

Posted 23 July 2008 - 11:12 PM

Quote

int fibonacci(int num, int num1, int num2);

Remove the semicolon.
Was This Post Helpful? 0

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}