Dr. A.P.J. Abdul Kalam Technical University, Lucknow
KCS151 / KCS251 Programming for Problem Solving - Using C Language
Lab Exercises
10. Write a program to find the sum of the following series 1-X1/1!+X2/2!- ... Xn/n!
/*
File: Prgrm10.c
Author: Aditya Saini
Date: Jan 16, 2021
Description: Program to find the sum of the following series 1 - X1/1! + X2/2! - ... Xn/n!
*/
#include <stdio.h>
#include <math.h>
int main (void)
{
int n;
int x;
double fact;
double sum;
int i, j;
//Input number of terms & x
printf ("Input number of terms: ");
scanf ("%d", &n);
printf ("Input x: ");
scanf ("%d", &x);
//Calculate the sum of series
sum = 1;
fact = 1;
for (i = 1; i <= n; i++)
{
fact = 1;
for (j = 1; j <= i; j++)
fact = fact * j;
sum = sum + pow (-x, i) / fact;
}
//Print result
printf ("Sum of Series: %.2lf", sum);
};
Output
Input number of terms: 4
Input x: 3
Sum of Series: 1.38
No comments:
Post a Comment
Please do not post spam links.