C # function 3 recursion

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Namespace ConsoleApplication1
{
Class Program
{
To understand recursion, first understand recursion. (It's just a joke)
Recursion, as the name implies, is handed to return, so repeatedly, until a certain condition is not met. and function recursive means function call function itself. Here's an example with code:

static int Funtion (int n)//Use this function to implement factorial, n to indicate the number of factorial
{
if (n <= 1)
return 1;
Else
return n * Funtion (n-1);
}

Then use a function to represent the Fibonacci sequence (the law of the Fibonacci sequence is that, starting with the third item, each item is the sum of the first two)

The static int Fei (int n)//n represents the item of the Fibonacci sequence.
{
if (n < 3)
return 1;
Else
Return Fei (n-1) + Fei (n-2);
}

static void Main (string[] args)
{
Call the above factorial function.
Console.WriteLine ("Please enter the number of times you want factorial:");
Console.WriteLine (Funtion (Convert.ToInt32 (Console.ReadLine ()));

Call the Fibonacci sequence function.
Console.WriteLine ("Please enter how many items you want to implement");
int n = Convert.ToInt32 (Console.ReadLine ());
Use the For statement to print out each item.
for (int i = 1; I <= n; i++)
{
Console.Write ("{0}\t", Fei (i));
if (i% 5 = = 0)
Console.WriteLine ();
}
}
}
}
/* Run the results as follows:
Please enter the number of times you want factorial:
10
3628800
------------------------
Please enter how many items you want to implement
30
1 1 2) 3 5
8 13 21) 34 55
89 144 233) 377 610
987 1597 2584) 4181 6765
10946 17711 28657) 46368 75025
121393 196418 317811) 514229 832040
*/

C # function 3 recursion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.