Solution:
#include<math.h>
#include<stdio.h>
int fact(int index)
{
int f = 1, i;
for(i = 1; i <= index; i ++)
{
f = f*i;
}
return f;
}
void main()
{
int x = 1;
int n = 3;
double sum = 0,term=-1, m;
// Sum of n-1 terms starting from 2nd term
int i;
for (i = 1; i <= n; i++) {
term = term * (-1);
m = term * pow(x, i) / fact(i);
sum = sum + m;
}
printf("\n%.4f", sum);
getch();
}
(Note: Write separate user defined function to calculate power and factorial)
0 Comments