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

Top 5 Contributors of the Month
Sandeep Singh
Melody Anderson
Eminent IT

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets


Posted By:papsantony       Posted Date: August 13, 2010    Points: 10    Category: Visual Studio    URL: http://www.dotnetspark.com  


This is an example of recursion: a function that calls itself:

using System;

public class Program
static int Additional(int a)
if( a <= 1 )
return 1;
return a + Additional(a - 2);

static int Main()
Console.WriteLine("Additional: {0}\n", Additional(5));
Console.WriteLine("Additional: {0}\n", Additional(11));
Console.WriteLine("Additional: {0}\n", Additional(28));
Console.WriteLine("Additional: {0}\n", Additional(55));
return 0;

This would produce:

Additional: 9

Additional: 36

Additional: 211

Additional: 784

Press any key to continue . . .



Further Readings:


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