Write a C Program To Use recursive calls to evaluate f(x) = x – x3/3! + x5/5! – x7/7! + ……
1: #include<stdio.h>
2: #include<conio.h>
3: int main()
4: {
5: float series(float,int),x;
6: int n;
7: clrscr();
8: printf("\nEnter X:");
9: scanf("%f",&x);
10: printf("\nEnter n:");
11: scanf("%d",&n);
12: printf("\nAns %f",series(x,n));
13: getch();
14: return 0;
15: }
16: float series(float x,int n)
17: {
18: long factorial(int);
19: float power(float,int);
20: float sum=0;
21: int i,s=1;
22: for(i=1;i<=n;i+=2)
23: {
24: sum+=(power(x,i)/factorial(i))*s;
25: s*=-1;
26: }
27: return sum;
28: }
29: float power(float x, int y)
30: {
31: float p=1;
32: int i;
33: for(i=1;i<=y;i++)p*=x;
34: return p;
35: }
36: long factorial(int p)
37: {
38: long f=1;
39: int i;
40: for(i=1;i<=p;i++)f*=i;
41: return f;
42: }
No comments:
Post a Comment