First:
n!/n = (n*(n-1)*..*1)/n = (n-1)*(n-2)*..*1 = (n-1)!
hence your task, for a given
n
, is finding the sum of
0! + 1! + .. + (n-1)!
Then, your factorial function is broken, because
z
is uninitialized.
Try
#include <stdio.h>
int fact(int);
int main()
{
int n, sum;
printf("enter the number ");
if ( scanf("%d",&n) != 1)
return -1;
sum = 0;
for(int i=0; i < n; ++i)
{
sum += fact(i);
}
printf("the sum is %d\n",sum);
}
int fact(int x)
{
int f = 1;
for (int i = 1; i <= x; ++i)
{
f *= i;
}
return f;
}