As I see you have -not surprisingly-

**some bugs**in your code.

C++

int fact(int x) { int z,f=0; // for(z=x-z;z=x;z--) <= the buggy code for(z=0;z<x;z--)

additional tip: use better names, factorial for the function.

See more:

/*Write a program in C to find the sum of the series 1!/1+2!/2+3!/3+4!/4+5!/5 using the function. Go to the editor Expected Output : The sum of the series is : 34 */ #include <stdio.h> int fact(int); main() { int i,y,sum=0,n; printf("enter the number end of the series"); scanf("%d",&i); printf("entrr the number "); scanf("%d",&n); for(i;i<=n;i++) { y=fact(i)/i; sum=sum+y; } printf("the sum is %d",sum); } int fact(int x) { int z,f=0; for(z=x-z;z=x;z--) { f=f*z; } return (f); }

/*Write a program in C to find the sum of the series 1!/1+2!/2+3!/3+4!/4+5!/5 using the function. Go to the editor Expected Output : The sum of the series is : 34 */ #include <stdio.h> int fact(int); main() { int i,y,sum=0,n; printf("enter the number end of the series"); scanf("%d",&i); printf("entrr the number "); scanf("%d",&n); for(i;i<=n;i++) { y=fact(i)/i; sum=sum+y; } printf("the sum is %d",sum); } int fact(int x) { int z,f=0; for(z=x-z;z=x;z--) { f=f*z; } return (f); }

Use need to learn to use the debugger. Search some tutorial to figure it out.

As I see you have -not surprisingly-**some bugs** in your code.

additional tip: use better names, factorial for the function.

As I see you have -not surprisingly-

C++

int fact(int x) { int z,f=0; // for(z=x-z;z=x;z--) <= the buggy code for(z=0;z<x;z--)

additional tip: use better names, factorial for the function.

Permalink

Share this answer

First:

hence your task, for a given

Then, your factorial function is broken, because

Try

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 of0! + 1! + .. + (n-1)!

Then, your factorial function is broken, because

`z`

is uninitialized.Try

C

#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; }

Permalink

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900