ProgaOra/20231018/ConsoleApp2/Program.cs
2023-12-13 11:35:33 +01:00

40 lines
871 B
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
public static class Intkiterjesztesek
{
public static int Factorial(this int szam)
{
if (szam < 0) {
throw new ArgumentException("A negatív számoknak nincs faktoriálisa");
}
if (szam == 0 || szam == 1)
{
return 1;
}
int result = 1;
for (int i = 2; i <= szam; ++i) {
result *= i;
}
return result;
}
}
class Program
{
static void Main(string[] args)
{
int szam = 5;
int factorial = szam.Factorial();
Console.WriteLine($"{szam}! = {factorial}");
}
}
}