Write a C program to solve the given series using recursion
1+(2^x)/2+(4^x)/4+(8^x)/8+...
#include <stdio.h>
#include <math.h>
double series(double x, int n) {
if ( n <= 0 )
return 1;
return series(x, n-2) + pow(n, x) / n;
}
int main() {
double x, s;
int n;
printf("Enter x: ");
scanf("%lg", &x);
printf("Enter n: ");
scanf("%d", &n);
if (n%2 == 1) {
printf("Error n must be even number\n");
return 1;
}
s = series(x, n);
printf("The series is %lg\n", s);
return ;
}
Comments
Leave a comment