a. Cos(x) =1-x^2/2!+x^4/4!+...+(x^n/n!)
b. Sin(x) =1- x^3/3!+x^5/5!+...+(x^n/n!)
#include<stdio.h>
#include<conio.h>
float power(int x,int p)
{
int i;
float ans=1.0;
for(i=0;i<p;i++)
ans*=x;
return ans;
}
float factorial(int n)
{
float ans=n;
while(n>1)
ans*=--n;
return ans;
}
void main()
{
char op;
int x,n,i,loop=1;
float ans;
clrscr();
while(loop)
{
printf("\n\nSelect your option\n\na.Find cos(x)\n\nb.Find sin(x)\n\nc.Exit\n");
op=getch();
switch(op)
{
case 'a':
printf("\n\tcos(x)\n\nEnter the value of x : ");
scanf("%d",&x);
printf("\nEnter the value of n : ");
scanf("%d",&n);
ans=1-power(x,2)/factorial(2);
for(i=4;i<=n;i+=2)
ans+=power(x,i)/factorial(i);
printf("\n\tcos(%d)=%f",x,ans);
break;
case 'b':
printf("\n\tsin(x)\n\nEnter the value of x : ");
scanf("%d",&x);
printf("\nEnter the value of n : ");
scanf("%d",&n);
ans=1-power(x,3)/factorial(3);
for(i=5;i<=n;i+=2)
ans+=power(x,i)/factorial(i);
printf("\n\tsin(%d)=%f",x,ans);
break;
case 'c':
loop=0;
printf("\n\tPress Any key to exit");
break;
default :
printf("invalid option");
}
getch();
}
}