How to call the recursive method of C #
This article mainly introduces "how to call the recursive method of C#". In daily operation, I believe many people have doubts about how to call the recursive method of C#. Xiaobian consulted various materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "how to call the recursive method of C#"! Next, please follow the small series to learn together!
recursive method call
A method can call itself. This is called recursion. The following example uses a recursive function to calculate the factorial of a number:
examples
using System;
namespace CalculatorApplication
{
class NumberManipulator
{
public int factorial(int num)
{
/* Local variable definition */
int result;
if (num == 1)
{
return 1;
}
else
{
result = factorial(num - 1) * num;
return result;
}
}
static void Main(string[] args)
{
NumberManipulator n = new NumberManipulator();
//call factorial method
Console.WriteLine("factorial of 6 is: {0}", n.factorial(6));
Console.WriteLine("factorial of 7 is: {0}", n.factorial(7));
Console.WriteLine("factorial of 8 is: {0}", n.factorial(8));
Console.ReadLine();
}
}
}
When the above code is compiled and executed, it produces the following results:
6 factorial is: 7207 factorial is: 50408 factorial is: 40320 This, on the "C#recursive method how to call" the study is over, I hope to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!