1. #include
#include
#include
#include
//FIND FACTORIAL
double fact(int n)
{
int i;
double prod = 1.;
for(i = 1; i <= n; i++)
prod = prod * i;
return prod;
}
//FIND x TO THE POWER OF n
double pow(double x, int n)
{
int i;
double prod = 1.;
for (i = 0; i < n; i++)
prod = prod * x;
return prod;
}
//CALCULATE SIN(x)
double sineFun(double x)
{
double sum = 0.;
int i, sign = 1;
for (i = 0; i < 21; i++) {
sum = sum + sign * pow(x, 2 * i + 1) / fact(2 * i +1);
sign = -sign;
}
return sum;
}
//FUNCTION TO FIND COS(X) USING TAYLOR’S SERIE
double coseFun(double x)
2. {
}
//FUNCTION TO FIND EXP(X) USING TAYLOR’S SERIES
double expoFun(double x)
{
}
int main() {
double x;
char more;
do {
printf(" tttInput X: ");
scanf("%lf", &x);
printf(" tttt LibraryResult t MyResult");
printf(" tt sin( %.1lf)t %lft%lf", x, sin(x), sineFun(x));
printf(" tt cos( %.1lf)t %lft%lf", x, cos(x), coseFun(x));
printf(" tt exp( %.1lf)t %lft%lf", x, exp(x), expoFun(x));
printf(" ttttDo more (Y/N)?:");
scanf(" %s", &more);
}while(more=='y'||more=='Y');
} Turbo C-tc Input : 2.4 sin cos( 2.40) exp 2.4 LibraryResult 0.675463 -6.737394 11.823176
MyResult 0.675463 0.737394 11.823176 Do nore Y/N? y Input x: -2.4 MyResult -0.675463 -
0.737394 0.090218 LibraryResult sin? y Input x: 4.4 LibraryRe sult MyResult sin? y Input : 4.4
sin
Solution
Please find the required methods along with its output. Please see the comments against each line
to understand the step.
NOTE: while compiling please use the option -lm to avoid undefined reference error.
3. command : gcc -o main *.c -lm
#include
#include
#include
#include
#define PI 3.1415
//FIND FACTORIAL
double fact(int n)
{
int i;
double prod = 1.;
for(i = 1; i <= n; i++)
prod = prod * i;
return prod;
}
//FIND x TO THE POWER OF n
double power(double x, int n)
{
int i;
double prod = 1.;
for (i = 0; i < n; i++)
prod = prod * x;
return prod;
}
//CALCULATE SIN(x)
double sineFun(double x)
{
double sum = 0.;
int i, sign = 1;
for (i = 0; i < 21; i++) {
sum = sum + sign * power(x, 2 * i + 1) / fact(2 * i +1);
sign = -sign;
}
return sum;
}
//FUNCTION TO FIND COS(X) USING TAYLOR’S SERIE
4. double coseFun(double x)
{
double value = 1.0;
for (int i = 2, j = 1;i < 21;i += 2, j++ )
{
value += ( double ) power( -1.0, j ) * power( x, i ) / fact( i ); //taylor series expansion for cosine
series
}
return value;
}
//FUNCTION TO FIND EXP(X) USING TAYLOR’S SERIES
double expoFun(double x)
{
int i = 1;
float ex = 1;
while ( i < 21 )
{
ex += ( float ) power( x, i ) / fact( i ); //taylor series expansion for exponential
++i;
}
return ex;
}
int main() {
double x;
char more;
do {
printf(" tttInput X: ");
scanf("%lf", &x);
printf(" tttt LibraryResult t MyResult");
printf(" tt sin( %.1lf)t %lft%lf", x, sin(x), sineFun(x));
printf(" tt cos( %.1lf)t %lft%lf", x, cos(x), coseFun(x));
printf(" tt exp( %.1lf)t %lft%lf", x, exp(x), expoFun(x));