GTU Programming for Problem Solving Practical-28
// 28. Write a program to evaluate the series sum=1-x+x^2/2!-x^3/3!+x^4/4!......-x^9/9!
#include <stdio.h>
#include <math.h>
float fact(float a);
int main(void)
{
float sum = 0;
int n, x;
printf("\nEnter the number till ypu wanted series");
scanf("%d", &n);
printf("\nEnter the value of x");
scanf("%d", &x);
printf("\n1");
for (int i = 1; i <= n; i++)
{
if (i % 2 == 0)
{
printf(" + %d^%d/%d!", x, i, i);
}
else
{
printf(" - %d^%d/%d!", x, i, i);
}
sum = sum + (pow(x, i) / fact(i));
}
printf(" = %f", sum);
return 0;
}
float fact(float a)
{
float ans = 1;
for (int i = 1; i <= a; i++)
{
ans = ans * i;
}
return ans;
}
Output:
Enter the number till ypu wanted series10
Enter the value of x2
1 - 2^1/1! + 2^2/2! - 2^3/3! + 2^4/4! - 2^5/5! + 2^6/6! - 2^7/7! + 2^8/8! - 2^9/9! + 2^10/10! = 6.388995
0 Comments