Use recursive function calls to evaluate
f(x) = x - power 3 to x/3! + power 5 to x/5! - power 7 to x/7! + .....
#include <stdio.h>
#include <math.h>
calculate(long double x);
fact(long double y);
void main()
{
long double x, ans;
clrscr();
printf("Enter the value of X:");
scanf("%Lf",&x);
calculate(x);
getch();
}
calculate(long double x)
{
int j=2,i,f;
f=x;
for(i=3;i<11;i=i+2)
{
if(j%2==0)
x=x-pow(f,i)/(fact(i));
else
x=x+pow(f,i)/(fact(i));
j++;
}
printf("Series of your no. = %Lf",x);
return(0);
}
fact(long double y)
{
long double fac;
if(y==1)
return(1);
else
fac=y*fact(y-1);
return(fac);
}
OUTPUT
Enter the value of X : 2
Series of your no. : 0.891059
f(x) = x - power 3 to x/3! + power 5 to x/5! - power 7 to x/7! + .....
#include <stdio.h>
#include <math.h>
calculate(long double x);
fact(long double y);
void main()
{
long double x, ans;
clrscr();
printf("Enter the value of X:");
scanf("%Lf",&x);
calculate(x);
getch();
}
calculate(long double x)
{
int j=2,i,f;
f=x;
for(i=3;i<11;i=i+2)
{
if(j%2==0)
x=x-pow(f,i)/(fact(i));
else
x=x+pow(f,i)/(fact(i));
j++;
}
printf("Series of your no. = %Lf",x);
return(0);
}
fact(long double y)
{
long double fac;
if(y==1)
return(1);
else
fac=y*fact(y-1);
return(fac);
}
OUTPUT
Enter the value of X : 2
Series of your no. : 0.891059
No comments:
Post a Comment