# Generate Fibonacci Series Using Recursion

Page 1 of 1

## 4 Replies - 56220 Views - Last Post: 11 May 2008 - 09:33 AMRate 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=51604&amp;s=bd10d9250e72a06d7e0f78ebb0841bdb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 crafty

• New D.I.C Head

Reputation: 1
• Posts: 24
• Joined: 10-May 08

# Generate Fibonacci Series Using Recursion

Posted 10 May 2008 - 03:48 AM

```namespace fabi
{
class fab
{
public static int fibonacci(int n)
{
int previous = -1;
int next = 1;
for (int i = 0; i < n; i++)
{
int sum = next + previous;
previous = next;
next = sum;
Console.WriteLine(next);
}
return next;
}

static void Main(string[] a)
{
int num;
Console.WriteLine("Enter a number:");
num = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\n");
Console.WriteLine("The first " + num + " number(s) in the fibonacci series are: ");
fibonacci(num);

Console.Read();
}
}
}

```

I want to convert this program into generating Fibonacci Series using recursion. Please help.

Is This A Good Question/Topic? 0

## Replies To: Generate Fibonacci Series Using Recursion

### #2 baavgai

• Dreaming Coder

Reputation: 6295
• Posts: 13,461
• Joined: 16-October 07

## Re: Generate Fibonacci Series Using Recursion

Posted 10 May 2008 - 07:58 AM

To show you would probably be to do it.

So, here's a hint. The following two methods give identical output.

```// non recursion
void ShowNumsLoopy(int n) {
for (int i = 0; i < n; i++) {
Console.WriteLine(i);
}
}

// recursion
// in recursion, there's usually some kind of setup before the thing gets started.
void ShowNumsRecusion(int n) {
ShowNumsRecusion(n, 0);
}
// the work is here
void ShowNumsRecusion(int n, int i) {
if (i < n) {
Console.WriteLine(i);
ShowNumsRecusion(n, i - 1);
}
}

```

Hope this helps.
Was This Post Helpful? 0

### #3 crafty

• New D.I.C Head

Reputation: 1
• Posts: 24
• Joined: 10-May 08

## Re: Generate Fibonacci Series Using Recursion

Posted 10 May 2008 - 09:57 PM

Hey thanks. I finished my program.

But I hv a doubt :
```ShowNumsRecusion(n, i - 1);
```

It went into infinite loop when I used it, instead I used :
```ShowNumsRecusion(n, i + 1);
```

Was This Post Helpful? 0

### #4 baavgai

• Dreaming Coder

Reputation: 6295
• Posts: 13,461
• Joined: 16-October 07

## Re: Generate Fibonacci Series Using Recursion

Posted 11 May 2008 - 08:36 AM

Oops. Yeah, that way.

A more elegant example was this:

```void ShowNumsRecusion(int n) {
if (n<0) { return: }
ShowNumsRecusion(n-1);
Console.WriteLine(n);
}

```

But it contains n inclusive, which isn't exactly the same, so I rewrote it. Bugs are funny that way. Sorry.
Was This Post Helpful? 0

### #5 crafty

• New D.I.C Head

Reputation: 1
• Posts: 24
• Joined: 10-May 08

## Re: Generate Fibonacci Series Using Recursion

Posted 11 May 2008 - 09:33 AM

This is very elegant and simple. Thanks again.

This post has been edited by crafty: 11 May 2008 - 09:33 AM

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; }