include include include include FIND FACTORIAL double factin
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
//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)
{
}
//FUNCTION TO FIND EXP(X) USING TAYLOR’S SERIES
double expoFun(double x)
{
}
int main() {
double x;
char more;
do {
printf(\"\ \\t\\t\\tInput X: \");
scanf(\"%lf\", &x);
printf(\"\ \\t\\t\\t\\t LibraryResult \\t MyResult\");
printf(\"\ \\t\\t sin( %.1lf)\\t %lf\\t%lf\", x, sin(x), sineFun(x));
printf(\"\ \\t\\t cos( %.1lf)\\t %lf\\t%lf\", x, cos(x), coseFun(x));
printf(\"\ \\t\\t exp( %.1lf)\\t %lf\\t%lf\", x, exp(x), expoFun(x));
printf(\"\ \ \\t\\t\\t\\tDo more (Y/N)?:\");
scanf(\" %s\", &more);
}while(more==\'y\'||more==\'Y\');
}
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.
command : gcc -o main *.c -lm
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#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
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(\"\ \\t\\t\\tInput X: \");
scanf(\"%lf\", &x);
printf(\"\ \\t\\t\\t\\t LibraryResult \\t MyResult\");
printf(\"\ \\t\\t sin( %.1lf)\\t %lf\\t%lf\", x, sin(x), sineFun(x));
printf(\"\ \\t\\t cos( %.1lf)\\t %lf\\t%lf\", x, cos(x), coseFun(x));
printf(\"\ \\t\\t exp( %.1lf)\\t %lf\\t%lf\", x, exp(x), expoFun(x));
printf(\"\ \ \\t\\t\\t\\tDo more (Y/N)?:\");
scanf(\" %s\", &more);
}while(more==\'y\'||more==\'Y\');
}
--------------------------------------
OUTPUT:
Input X: 2.4
LibraryResult MyResult
sin( 2.4) 0.675463 0.675463
cos( 2.4) -0.737394 -0.737394
exp( 2.4) 11.023176 11.023178
Do more (Y/N)?:Y
Input X: -2.4
LibraryResult MyResult
sin( -2.4) -0.675463 -0.675463
cos( -2.4) -0.737394 -0.737394
exp( -2.4) 0.090718 0.090718
Do more (Y/N)?:Y
Input X: 4.4
LibraryResult MyResult
sin( 4.4) -0.951602 -0.951602
cos( 4.4) -0.307333 -0.307333
exp( 4.4) 81.450869 81.450882
Do more (Y/N)?:Y
Input X: -4.4
LibraryResult MyResult
sin( -4.4) 0.951602 0.951602
cos( -4.4) -0.307333 -0.307333
exp( -4.4) 0.012277 0.012279
Do more (Y/N)?:N




