Change this code so that you do division BEFORE doing multip
Change this code so that you do division BEFORE doing multiplication for each item.
code:
#define _CRT_SECURE_NO_WARNINGS
#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;
int i, j;
for (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\\tLibraryResult \\t MyResult\");
printf(\"\ \\t\\t sin( %.2lf)\\t %1lf\\t%1lf\", x, sin( x), sineFun( x));
printf(\"\ \\t\\t cos( %.2lf)\\t %1lf\\t%1lf\", x, cos( x), coseFun( x));
printf(\"\ \\t\\t exp( %.2lf)\\t %1lf\\t%1lf\", x, exp( x), expoFun( x));
printf(\"\ \ \\t\\t\\t\\tDo more (Y/N)?:\");
scanf(\" %c\", &more);
}while(more==\'y\'||more==\'Y\');
return 0;
}
Solution
#define _CRT_SECURE_NO_WARNINGS
#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;
int i, j;
for (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\\tLibraryResult \\t MyResult\");
printf(\"\ \\t\\t sin( %.2lf)\\t %1lf\\t%1lf\", x, sin( x), sineFun( x));
printf(\"\ \\t\\t cos( %.2lf)\\t %1lf\\t%1lf\", x, cos( x), coseFun( x));
printf(\"\ \\t\\t exp( %.2lf)\\t %1lf\\t%1lf\", x, exp( x), expoFun( x));
printf(\"\ \ \\t\\t\\t\\tDo more (Y/N)?:\");
scanf(\" %c\", &more);
}while(more==\'y\'||more==\'Y\');
return 0;
}
------------------------
output sample 1:-
Input X: 3
LibraryResult MyResult
sin( 3.00) 0.141120 0.141120
cos( 3.00) -0.989992 -0.989992
exp( 3.00) 20.085537 20.085539
Do more (Y/N)?:y
Input X: 7
LibraryResult MyResult
sin( 7.00) 0.656987 0.656987
cos( 7.00) 0.753902 0.757094
exp( 7.00) 1096.633158 1096.617432
---------------------------------------------------------------------------------------------
If you have any query, please feel free to ask.
Thanks a lot.



