hello everyone!
there seems to be some logical error in this program. i get unusually large values for any angle like even sine 90 degree.
Code:
/*c program to compute the sine series*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x,sum=0,fraction=0;
int n,i,j; /*the declarations*/
int factorial(int);
clrscr();
printf("\n Enter degree to calculate:");
scanf("%f",&x); /*reading the angle to be calculated*/
printf("\n Enter number of terms:");
scanf("%d",&n); /* upto how many terms to calculate the value*/
for(i=1,j=0;i<=n,j<n;i=i+2,j++)
{
fraction=pow(-1,j)*(float)(pow(x,i)/factorial(i));/*each term*/
sum+=fraction; /* summing up*/
}
printf("\n sine(%f)=%f",x,sum);
getch();
}
int factorial(int n) /* the factorial calculating function*/
{
int i;
int fact=1;
if(n==0)return 1;
else
{
for(i=1;i<=n;i++)
{
fact*=i;
}
return fact;
}
}
please help!