Write a C program to evaluate the series - 1 + x/1 + x/2! + x/3! + ... x/n!
/* calculate factorial for given data */
int fact(int data) {
int res = 1;
while (data > 1) {
res = res * data;
data--;
}
return res;
}
int main() {
float x, res = 1.0;
int n, i;
printf("Enter the value for x:");
scanf("%f", &x);
printf("Enter the value for n:");
scanf("%d", &n);
/* series evaluation */
for (i = 1; i <= n; i++) {
res = res + (x / fact(i));
}
printf("Result: %.3f\n", res);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter the value for x:10
Enter the value for n:10
Result: 18.183
Enter the value for x:10
Enter the value for n:10
Result: 18.183
No comments:
Post a Comment