응용 코드
using System;
using static System.Console;//Console단어를 생략(코드길이줄어둠)
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
WriteLine("n!을 계산합니다.");
//콘솔에서 숫자를 입력받아 정수로 변환하여 num에 저장한다.
Write(" 정수 n을 입력하세요: ");
int n = int.Parse(ReadLine());
//n!을 계산하기 위해 int형 변수 fact를 정의하고 1로 초기화.
int fact = 1;
//fact = 1*2*...*n으로 계산하므로 i를 1부터 n까지 반복하면서
for (int i = 2; i <= n; i++)
{
//fact에 곱해준다.
fact *= i;
}
//결과를 출력한다.
WriteLine("{0}! = {1}", n, fact);
}
}
}
결과
n!을 계산합니다.
정수 n을 입력하세요: 5
5! = 120