Question-Write a C function to evaluate the series
sin(x)=x-x^3/3!+x^5/5!-----
up to 5 decimal digits.
Solution-
essentially a good question.but,it has one problem.
/*"Here value of x must be entered in radians or converted into the radians ", i have chosen to convert the value to the radians then pass it through the function to calculate the sin value */
#include<stdio.h>
#include<math.h>
#define pi 3.14
/*if you don't know about this. don't worry . These are macros.good for defining constant , you can also write statement , but much use of them lead to bad results,notice that no semi colon and space between pi and value*/
int main()
{
int x;
float rad;
printf("Enter the value of x \n");
scanf("%d",&x);
rad=(pi/180)*x;
printf("Rad=%f\n",rad);
sine(&rad);
}
sine(float *x)
{
int i,j;
float sum,negsum=0.0,possum=0.0;
for(i=1;i<=9;i+=4)
possum=possum+(pow(*x,i)/factorial(i));
for(j=3;j<=9;j+=4)
negsum=negsum+(pow(*x,j)/factorial(j));
sum=possum-negsum;
printf("Answer=%f\n",sum);
}
factorial(int n)
{
int i,fact=1;
for(i=n;i>=1;i--)
fact=fact*i;
return(fact);
}
sin(x)=x-x^3/3!+x^5/5!-----
up to 5 decimal digits.
Solution-
essentially a good question.but,it has one problem.
/*"Here value of x must be entered in radians or converted into the radians ", i have chosen to convert the value to the radians then pass it through the function to calculate the sin value */
#include<stdio.h>
#include<math.h>
#define pi 3.14
/*if you don't know about this. don't worry . These are macros.good for defining constant , you can also write statement , but much use of them lead to bad results,notice that no semi colon and space between pi and value*/
int main()
{
int x;
float rad;
printf("Enter the value of x \n");
scanf("%d",&x);
rad=(pi/180)*x;
printf("Rad=%f\n",rad);
sine(&rad);
}
sine(float *x)
{
int i,j;
float sum,negsum=0.0,possum=0.0;
for(i=1;i<=9;i+=4)
possum=possum+(pow(*x,i)/factorial(i));
for(j=3;j<=9;j+=4)
negsum=negsum+(pow(*x,j)/factorial(j));
sum=possum-negsum;
printf("Answer=%f\n",sum);
}
factorial(int n)
{
int i,fact=1;
for(i=n;i>=1;i--)
fact=fact*i;
return(fact);
}
No comments:
Post a Comment