Here is the question I have on my HW The cosine of an angle can be computed from the following infinite series: 1- x^2/2!+x^4/4!-x^6/6! +......
Write a program that reads an angle x (in radians) from the keyboard, Then you are asked to write two functions: one has one input parameter to compute the factorial, for example (2!, 4!, etc.), the other one is to compute the cosine of the angle using this series until the absolute value of a term ( where a is a real even number, 2, 4, 6, 8,…) is greater than 0.0001. Finally, print out the value computed along with the value of cosine computed using the C library function. NO GLOBAL VARIABLES ARE ALLOWED.
So far i have this code, but for some reason my program always spits out a huge number any help would be appreciated.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int aFactorial(int);
int main(void)
{
int i =1,iX;
float fAns=1;
printf ("\nPlease enter a value for X: ");
scanf("%d",&iX);
while(pow(iX,i)/aFactorial(i) >=.0001)
{
fAns += pow(iX,i)/aFactorial(i);
i+=2;
}
printf("\nSum of the series is %f",fAns);
return 0;
}
int aFactorial(int iNumber)
{
int i,iFact=1;
for (i=1;i<=iNumber; ++i)
{
iFact *= i;
}
return (iFact);
}