Originally Posted by
Salem
> continue until the value of the next term becomes smaller than 10^-5
Is
if ( fabs(n)>0.00001 ) break;
Oh, and look up what the comma operator does in your for loop.
It isn't the same as && or ||
Code:
#include <stdio.h>
#include <conio.h>
#include <math.h>
float denominator (int);
int main()
{
float f,n,x,a,b,sum=0;
int i,j,counter=0;
printf ("Enter the number x for sin(x): ");
scanf ("%f",&x);
for (i=1 , j=1;i<=50 , j<=50;i++ , j+=2)
{
a=pow(-1,i+1) * pow(x,j) ;
b=denominator(j);
n=a/b;
f = fabs(n);
while(f>0.00001)
{
sum=sum+n;
counter++; break; }
}
printf ("sin(%f)= %f \n",x,sum);
printf ("No. of terms used = %d",counter);
getch();
}
float denominator (int j)
{
int m ;
float h =1;
for (m=1;m<=j;m++)
h=h*m;
return (h);
}
This is the right code !! Thanks a lot !!