using System;
// 예상 출력.
//Factorial of 5 is 120
Console.WriteLine(" 팩토리얼을 계산합니다 숫자를 입력해주세요");
int number = int.Parse(Console.ReadLine());
long fact = 0;
{
Console.Write($"{number}! -> ");
fact = 1; //1로 초기화
for (int j = 1; j <= number; j++)
{
fact = fact * j;
}
Console.WriteLine("입력 받은 숫자 :" + number);
Console.WriteLine("결과값"+$"{fact}");
}
int num, n;
Console.WriteLine(".....................[Application 1].....................\n\n");
Console.WriteLine("Please enter a number to get its factorial: ");
num = Convert.ToInt32(Console.ReadLine());
n = num; // Assign n to num
while (num > 0)
{
for (int i = n - 1; i > 0; i--)
{
n *= i;
}
Console.WriteLine("Factorial of {0}! = {1}\n", num, n);
num--;
출처 : https://stackoverflow.com/questions/29027000/c-sharp-to-calculate-factorial
while문을 사용한 팩토리얼 계산이다.
int FactorialTail(int n)
{
int acc = 1;
do{
if(n == 1) return acc;
acc = acc * n;
n = n - 1;
} while(true);
}
Console.WriteLine(" 팩토리얼을 계산합니다 숫자를 입력해주세요");
int number = int.Parse(Console.ReadLine());
int factorial = 0;
for (int i = 1; i >= 1; i--)
{
number *= i;
}
{
Console.WriteLine("입력 받은 숫자 :" + number);
Console.WriteLine("결과값 : " + factorial);
}
처음 구상하던 결과값이다. factorial값이 출력되지 않아 생각보다 복잡했다.