PDA

View Full Version : سوال: مجموع فاکتوریل های عدد وارد شده



Hamid2547
شنبه 12 آذر 1390, 23:09 عصر
به نظر شما مشکل این برنامه چیه؟این برنامه قراره عدد N رو دریافت و !S=1!+2!+3!...N رو حساب کنه،به طور مثال سه رو وارد کردیم باید 9 رو توی مقدار S بریزه ولی خروجی 15 میشه و نمیدونم چرا.

static void Main(string[] args)
{
int a = int.Parse(Console.ReadLine());
int result = 1;
int sumofall = 0;
for (int i = 1; i <= a;i++ )
{

for (int j = 1; j <= i;j++ )
{

result = result * j;

}
sumofall = sumofall + result;
}
Console.WriteLine("The sum of all fatorial is {0}", sumofall);
Console.ReadLine();
}

asadegha
شنبه 12 آذر 1390, 23:23 عصر
کدت رو اینطوری اصلاح کن درست میشه

static void Main(string[] args)
{
int a = int.Parse(Console.ReadLine());
int sumofall = 0;
for (int i = 1; i <= a;i++ )
{
int result = 1;
for (int j = 1; j <= i;j++ )
{

result = result * j;

}
sumofall = sumofall + result;
}
Console.WriteLine("The sum of all fatorial is {0}", sumofall);
Console.ReadLine();
}