Evaluate cosx=1-x^2/2!+x^4/4!............+x^n/n! .where x is in radian.[for loop]
/*Evaluate cosx=1-x^2/2!+x^4/4!............+x^n/n! .where x is in radian.[for loop]*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n;
float x, sum=1, a=1;
printf(" Enter the value of x : ");
scanf("%f",&x);
printf(" Enter the value of n : ");
scanf("%d",&n);
x=x*3.1415/180;
for(i=1;i<=n;i++)
{
a=a*(-1)*x*x/(2*i*(2*i-1)); // decorate same as series excluding 1 at beginning
sum=sum+a; // now add 1 to whole series (sum=1)
}
printf(" The value of Cosx is : %.4f", sum);
getch();
}
OUTPUT:
Enter the value of x :30
Enter the value of n :5
The value of Cosx is :0.8660
0 comments:
Post a Comment