1. Write a C++ program to evaluate the following expression: 1-(x/1!) + (x2/2!) - (x3/3!) + … +/- (xn/n!) given values for x and n. These values need to be properly declared and input from the keyboard. Properly document/comment the program. Show output after execution for different values of x and n (at least 5 pairs of values).
#include<iostream.h>
#include<cmath.h>
int fact(int n)
{
if(n==0) return 1;
else return n*fact(n-1);
}
void main()
{
int x,n;
double sum=1;
clrscr();
cout << " enter values of x and n";
cin >> x >> n;
for(int i=1; i<=n; i++)
{
if(i%2==0)
sum = sum+pow(x,i)/fact(i);
else
sum = sum-pow(x,i)/fact(i);
}
cout << " sum of series given by" << sum << endl;
getch();
}
No comments:
Post a Comment