.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ADO.NET >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Fibonacci using Recursion in C#

Posted By:shiv chandra       Posted Date: June 30, 2011    Points: 40    Category: ADO.NET    URL: http://www.dotnetspark.com  

This sample code show how to do Fibonacci using Recursion in C#
 

Basic definition of recursion is calling function itself. Here we have created function named Fibonacci. Function will call itself value of X is greater than 1.
static int Fibonacci (int x)
{
   Console.WriteLine ("Fibonacci = {0}", x);
   if (x <= 1)
   {
      return x;
   }
   return Fibonacci (x-1) + Fibonacci (x-2);
}
	 
static void Main( )
{
   Console.WriteLine ("Fibonacci = {0}", Fibonacci (5));
    
   // Wait for read one key
   Console.ReadKey();
}
In above example we have pass 5 as argument to Fibonacci function. You may read number from user and pass it to function.

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend