I'm trying to make a program to calculate sum of n terms of the series (x + (x^3)/3! + (x^5)/5! +...) -

#include<stdio.h>
#include<math.h>
main()
{
    int x,r,n,fct=1;
    float term,sum=0;
    printf("Enter the value for x:");
    scanf("%d",&x);
    printf("Enter the number of terms to calculate sum upto:");
    scanf("%d",&n);
    for(r=2(n-1)+1;r%2!=0;r++,n++) /*as r's value in nth term comes up to be 2(n-1)+1*/
    {
        fct=fct*r;
        term=pow(x,r)/fct;
        sum=sum+term;
    }
    printf("The sum of the series (x^r)/r is %f",sum);
}

but it is showing the error "called object is not a function or a function pointer".
What is it and how to fix it?

probably 2(n+1) that looks like a function call - the multiplication needs to be explicit as in 2*(n+1)

For future reference, if your program is throwing an error, it is always helpful to say what line is causing it. It may have been obvious in this case, but some people post very large blocks of code. The more information you can provide the more likely you are to get a quick response.

Also, i see you are using r%2!=0 as your loop test. What this says is to execute the loop as long as r is odd. I don't think this is what you want. I also think your starting value is incorrect. According to your initial statement I think you want to start at one and increment by one, calcultaing the new values of power and factorial, but only adding in to the series total when r is odd. There is no need to call the pow function for each iteration of the loop when you can do a simple multiply.

Typically a series notation is read like for i equals 1 to n, sum of... so it makes sense to structure your loop like

for (i=1, i<= n, i++)

My preference is to keep everything out of the for statement that isn't directly related to the control of the loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.