Sum of series.

This is the series here:
X+X^2/3!+X^3/5!+.......+X^n/(2N-1)!

I wrote the code but a compile time error occurs which says "call to the undefined function fact in the function main()"

PLS LEMME KNOW WHAT'S WRONG?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream.h>
#include<conio.h>
#include<math.h>

int main()
{ int x,n,sum,term;
cout<<"Enter the value of x";
cin>>x;
cout<<"Enter number of terms";
cin>>n;
for (int i=1;i<=n;i++)
term=pow(x,i)/fact(2*n-1);
sum+=term;
return sum;
}
7 issues in 15 lines:

1. replace #include <iostream.h> with #include <iostream>

2. replace cout with std::cout and cin with std::cin. Alternatively, add using namespace std; above your main.

3. Depending on your compiler, int pow(int, int) may not be available. Try replacing pow(x,i) with pow(x, (float)i ).

4. fact() is not a standard function. You'll need to write this. Something like this will work:
1
2
3
4
5
6
int fact(int x)
{
  int output = 1;
  while (x > 1)  output *= x--;
  return output;
}


5. You don't use anything in <conio.h>. You can remove that include.

6. sum is not initialized. In line 6, replace sum, with sum=0,.

7. You return sum. Why is that? return quits a function, in this case it end's your program with an exit code for the operating system. I think you want to replace line 14 with:
1
2
std::cout << sum;
return 0;
Last edited on
Topic archived. No new replies allowed.