please tell me how to compute the maclaurin series sinx=x-x^3/3!+x^5/5!-x^7/7!.....
I written below coding but i know it will not give correct output. can anyone help me to compute the series

#include<stdio.h>
#include<math.h>
Printf("Enter the n value");
scanf("%d", &n);
main()
{
float x,i;
float value,sinx;
printf("Enter the x value");
scanf("%d", &x);
for(i=1;i<=n;i=i+2)
{
value=pow(x,i);
sinx=x-value/factorial();
}
}

void factorial()
{
int j, fact=1;
for(j=1;j<n;j++)
{
fact=fact*j;
}
}

## All 10 Replies

#include<stdio.h>
#include<math.h>

int factorial( int );

int main()
{
int n;
double x,i;
double value, sinx;

value = sinx = 0.0;

printf("Enter the value for x and i");
scanf("%lf %d", &x, &n);

for( i=1; i<=n; i=i+2)
{
value = pow(x,i);
sinx += (x - value / factorial(i));
}

printf("Result - %f", sinx );

getchar();
return 0;
}

int factorial( int n )
{
int j, fact=1;

for( j=n; j>0; j-- )
fact = fact * j;

return fact;
}

Where is your code indendation and the code tags. There where quite a lot of mistakes which you have done in the code. Have a look at the above with some slite modification. If you don understand ask!

ssharish

<< beat me to it >> :)

Thanks for the reply, but still i have doubt regd how it will calculate for alternative + and - sign in series. how it works????

sinx=x-x^3/3!+x^5/5!-x^7/7!.....

code is returning zero value. how to produce non zero value by dividing a small number by big number

Well, you need to do some error checking there. Thats not a complete code as I said before. You need to work upon it. You need to do some error checking there. Anything divided by 0 is 0. What are the input values did you give?

ssharish

for( i=1; i<=n; i=i+2)
{
value = pow(x,i); --> this is computing power of x,i
sinx += (x - value / factorial(i)); --> here value is smaller than factorial(i) value, so it is producing zero value. Normally i use dot with that number to produce the correct result. for ex: 2/5 will be given as 2./5 then only it is producing correct result.

So in this case what i have to do produce correct result
}

Well, you could actually return of type double from your factorial function. You could have the denominator float as well to produce the right result. So try with return double and some modification in the factorial function by changing the data type of variable "fact" and do double calculation there instead of int.

And what inputs are you giving for variables x and n?

ssharish

Some notes:
1. Never use float for math calculations (as in original post). Use double type only.
2. Try to avoid factorial function calculations (especially as integer). This function has too fast growth.
3. Try to use recurrent dependiences.

For example (it's not ideal solution, of course):

double sinRough(double x)
{
const double eps = 1e-6;
double x2 = (x*x);
double fact = 1.0 / 6.0;
double sinx = x-x*x2*fact;
double next = sinx;
double step = 4.0;

const int maxi = 1000;
int neg = 0;

for (int i = 0;fabs(next) > eps && i < maxi;step += 2.0,++i, neg ^= 1)
{
next *= x2;
next /= step * (step + 1.0);
sinx += (neg?-next:next);
}
return sinx;
}

Oh, I'm sorry, advice #4:
4. Don't use expensive pow() function if you can do it differently.

Good luck!

Hi, this is the algorithm to calculate pi,
Pi = 4-4/3+4/5-4/7+4/9...= 4*((-1)^n+1)/2n-1
It's very similar to your need

#include<stdio.h>
#include<math.h>

int main()
{
float delta; //precision
float pi=0,temp;
int n=1;
scanf("%f",&delta);
do
{
temp = (pow(-1.0,n+1)/(2*n-1)*4);
pi+=temp;
n+=1;
if (temp<0)
temp*=-1;
}while (temp>delta);
printf("%f",pi);
return 0;
}

I hope I helped you
BTW… the delta must be les then zero

Be a part of the DaniWeb community

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