OK, this is the code. Input i want to have is a natural number, output i want a value of an kth term of a sequence calculated according to the following formula:
a_0=1 and
a_(k+1)=a_k+(1/k!)
In this program, no matter which number i will type in i always recieve an answer equal to 2. How to fix this?
Code:
#include "stdafx.h"
unsigned long long int factorial(unsigned int n)
{
if ((n == 0) || (n == 1))
return(1);
return(n*factorial(n - 1));
}
float series(int n)
{
float F0 = 1;
float F;
unsigned int i;
if (n == 0)
return(1);
for (i = 1; i <= n; i++)
{
F = F0 + 1/factorial(i);
F0 = F;
}
return(F);
}
int _tmain(int argc, _TCHAR* argv[])
{
int k;
printf_s("give number:\n");
scanf_s("%d", &k);
printf("series(%d) = %f\n", k, series(k));
return(0);
}