Saturday, 12 March 2011

Use recursive calls to evaluate f(x) = x – x3/3! + x5/5! – x7/7! + ……

#include<stdio.h>
#include<conio.h>
int main()
{
float series(float,int),x;
int n;
clrscr();
printf("\nEnter X:");
scanf("%f",&x);
printf("\nEnter n:");
scanf("%d",&n);
printf("\nAns %f",series(x,n));
getch();
return 0;
}
float series(float x,int n)
{
long factorial(int);
float power(float,int);
float sum=0;
int i,s=1;
for(i=1;i<=n;i+=2)
{
sum+=(power(x,i)/factorial(i))*s;
s*=-1;
}
return sum;
}
float power(float x, int y)
{
float p=1;
int i;
for(i=1;i<=y;i++)p*=x;
return p;
}
long factorial(int p)
{
long f=1;
int i;
for(i=1;i<=p;i++)f*=i;
return f;
}
Output
Enter X:1.2
Enter n:5
Ans 0.932736

No comments:

Post a Comment