‘C’ function to calculate x^y. Use this function to calculate the sum of first n terms of the series x + x^3/3 + x^5/5 + ...
C File =>
/* Write a ‘C’ function to calculate xy. Use this function to calculate the sum of first n terms of the series x + x^3/3 + x^5/5 + ... */
#include<stdio.h>
int main()
{
int n,x,i,sum=0;
// defining function to compute x^y
int cal_pow(int x, int y)
{
if (y == 0)
return 1;
else if (y%2 == 0)
return cal_pow(x, y/2)*cal_pow(x, y/2);
else
return x*cal_pow(x, y/2)*cal_pow(x, y/2);
}
printf("\n Enter value of x : ");
scanf("%d",&x);
printf("\n Enter value of n : ");
scanf("%d",&n);
for(i=1;i<=n;i=i+2)
sum=sum+(cal_pow(x,i)/i);
printf("\n Sum of series = %d",sum);
return 0;
}
Comments
Post a Comment
Please do not enter any spam link in message.