ProgaOra/20231018/ConsoleApp2/Program.cs

40 lines
910 B
C#
Raw Normal View History

2024-01-19 14:06:27 +00:00
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}");
}
}
}