C# 메서드를 재귀적으로 호출하기 위해 다음 코드를 실행할 수 있습니다. 여기서 숫자의 계승은 재귀 함수 display()를 사용하여 찾는 것입니다.
값이 1이면 Factorial이 1이므로 1을 반환합니다.
if (n == 1) return 1;
그렇지 않은 경우 1값이 5인 경우 다음 반복에 대해 재귀 함수가 호출됩니다!
Interation1: 5 * display(5 - 1); Interation2: 4 * display(4 - 1); Interation3: 3 * display(3 - 1); Interation4: 4 * display(2 - 1);
다음은 C# 메서드를 재귀적으로 호출하는 전체 코드입니다.
예
using System;
namespace MyApplication {
class Factorial {
public int display(int n) {
if (n == 1)
return 1;
else
return n * display(n - 1);
}
static void Main(string[] args) {
int value = 5;
int ret;
Factorial fact = new Factorial();
ret = fact.display(value);
Console.WriteLine("Value is : {0}", ret );
Console.ReadLine();
}
}
} 출력
Value is : 120