숫자의 계승은 아래 예에서 재귀 함수 checkFact()를 사용하여 찾는 것입니다 -
값이 1이면 Factorial이 1 -
이므로 1을 반환합니다.if (n == 1) return 1;
그렇지 않은 경우 값 5를 원하면 다음 반복에 대해 재귀 함수가 호출됩니다!
Interation1: 5 * checkFact (5 - 1); Interation2: 4 * checkFact (4 - 1); Interation3: 3 * checkFact (3 - 1); Interation4: 4 * checkFact (2 - 1);
재귀를 사용하여 계승을 계산하려면 위에서 수행된 작업을 보여주는 다음 코드를 실행하려고 시도할 수 있습니다. -
예시
using System;
namespace Demo {
class Factorial {
public int checkFact(int n) {
if (n == 1)
return 1;
else
return n * checkFact(n - 1);
}
static void Main(string[] args) {
int value = 9;
int ret;
Factorial fact = new Factorial();
ret = fact.checkFact(value);
Console.WriteLine("Value is : {0}", ret );
Console.ReadLine();
}
}
} 출력
Value is : 362880