Thursday, 19 June 2014

C++ program to generate exponential series



Here is a C++ program to generate exponential series


 SOURCE CODE OUTPUT
#include<iostream>
#include<math.h>
using namespace std;
long double factorial(int n)
{
    long double fact=1;
    while(n>0)
    {
        fact*=n;
        n--;
    }
    return fact;
}
int main()
{
    long n,x;
    float an=0;
    cout<<"Enter first term (e^x) ";
    cin>>x;
    cout<<"Enter the value of n ";
    cin>>n;
    for(int y=0;y<=n;y++)
        an+=1*pow(x,y)/(1.0*factorial(y));
    cout<<"e^"<<x<<" = "<<an;
    return 0;
}

Any questions regarding to program please write in comments.

No comments:

Post a Comment