PDA

View Full Version : سوال: نوشتن فاکتوریل با Interface در C#



محمد255
دوشنبه 05 تیر 1391, 19:30 عصر
سلام
من فاکتوریل رو معمولی با C# اینطوری مینویسم:


Class Program
Public int fact (int n
int result
if (n==1) return 1
result = fact*(fact-n)*n
static void main
int no
factorial obj=new factorial
no = convert.to int 32 (console.readline
console.writeline (obj.fact(no


حالا میخواستم ببینم با Interface همین برنامه رو چطوری میشه نوشت؟
خیلی ممنونم.

Mahmoud.Afrad
دوشنبه 05 تیر 1391, 21:35 عصر
متدی که نوشتید اشتباهه که!!


interface f
{
Int64 fact(int n);
}

class Class4 : f
{
public Int64 fact(int n)
{
if (n == 1)
return 1;
else
return fact(n - 1) * n;
}
}

Shahim
پنج شنبه 06 تیر 1392, 18:33 عصر
اینطوری بهتر نیست:

public static ulong Factorial(ulong x)
{
ulong z = 1;
for (ulong i = 2; i <= x; i++)
{
z *= i;
}
return z;
}