4 Replies - 52809 Views - Last Post: 11 May 2008 - 09:33 AM Rate Topic: -----

#1 crafty  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5761
  • View blog
  • Posts: 12,574
  • 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. :P

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  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5761
  • View blog
  • Posts: 12,574
  • Joined: 16-October 07

Re: Generate Fibonacci Series Using Recursion

Posted 11 May 2008 - 08:36 AM

Oops. :P 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  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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