Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write two functions to compute the values of cos x and sin x respectively using

ID: 3679477 • Letter: W

Question

Write two functions to compute the values of cos x and sin x respectively using the following Taylor series (x is any positive float number represents in radian). Then, use these functions in main function to compute the following equation. Your program should ask user the following option: Press t/T for Tan x Press s/S for Sin x Press c/C for Cos x Press * for exit the program. Based on the input provided by the user, the program should ask user the value of x in radian and the value of term. Then, your program should print the result as shown in figure. Once, result is shown, the program will further show the menu as shown above until user chooses * as input to exit the program. For a wrong choice from the menu (other than t/T, s/S. c/C and *, your program should notify Wrong input and allow user to input his/her choice again). (Note: You should not use predefined sin and cos function; No points will be assigned if you use predefined sin and cos functions from math.h)

Explanation / Answer

#include<stdio.h>
#include<math.h>
#include<conio.h>

//declaring functions

float f(int);

float cos(float);

float sin(float);

float tan(float);
int main()
{

//declaring variables

char o;

int i,j;
float num,num1,sum_sin=0,sum_cos=1;
clrscr();

//asking user for valid input

printf("Press t/T for Tan x ");

printf("Press s/S for Sin x");

printf("Press c/C for Cos x");

printf("Press * for exit the program ");

//reading i/p

scanf("%c",&o);

while(true)

{

if(o== 't' ||'T')

{

printf("ENTER ARGUMENT IN RADIANS : ");
scanf("%f",&num1);

printf("Tan value %f is %f : " ,num1,tan(num1));

}

elseIf(o== 'c' ||'C')       

{

printf("ENTER ARGUMENT IN RADIANS : ");
scanf("%f",&num1);

printf("cos value %f is %f : " ,num1,cos(num1));

}

elseIf(o== 's' ||'S')       

{

printf("ENTER ARGUMENT IN RADIANS : ");
scanf("%f",&num1);

printf("sin value %f is %f : " ,num1,sin(num1));

}

elseIf(o=='*')

{

return true;

}

else

   printf("Enter correct option");

    }//while

    return 0;

}//main

float f(int i)
{
int j;
float fact=1;
for(j=0;j<i;j++)
fact*=(j+1);
return fact;
}

float cos(float num)

{

for(i=2,j=1;i<20;i=i+2,j++)
{
sum_cos+=pow(-1,j)*pow(num,i)/f(i);
}

return sum_cos;

}

float sin(float num)

{

for(i=0,j=0;i<20;i=i+2,j++)
{
sum_sin+=pow(-1,j)*pow(num,i+1)/f(i+1);
}

return sum_sin;

}

float tan(float num)

{

float v= sin(num)/ cos(num);

return v;

}