‘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

Popular posts from this blog

Write a program to allocate memory dynamically for n integers. Accept the elements and calculate their sum and average

C program to accept names of n cities and search for city named “Pune”.