C++ program for Exponential Series

Before going to the program for Exponential Series first let us understand what is a Exponential Series?

Exponential Series:

                Exponential Series is a series which is used to find the value of ex.

The formula used to express the ex as Exponential Series is

exponential-series

Expanding the above notation, the formula of Exponential Series is

exponential-series1

For example,

                Let the value of x be 3.

exponential-series2

So, the value of e3 is 20.0855

Program code for Exponential Series in C:

/* Program for Exponential Series */
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>

void main()
{
    int i, n;
    float x, sum=1, t=1;
    clrscr();
    
    cout<<" Enter the value for x : ";
    cin>>x;
    
    cout<<" Enter the value for n : ";
    cin>>n;
    
    /* Loop to calculate the value of Exponential */
    for(i=1;i<=n;i++)
    {
        t=t*x/i;
        sum=sum+t;
    }
    
    cout<<" The Exponential Value of "<<x<<" = "<<setprecision(4)<<sum;
    getch();
}

Note: setprecision(4) is used to set the floating point number upto 4 decimal points.

     iomanip.h is a header file which contains the setprecision() function.

Related: C++ program for Sine Series

Working:

  • First the computer reads the value of  ‘x’ and ‘n’ from the user.
  • Then using for loop the value of ex is calculate.
  • Finally the value of ex is printed.

Related: C++ program for Cosine Series

Step by Step working of the above Program Code:

Let us assume that the user enters the value of ‘x’ as 2 and ‘n’ as 5.

  1. It assigns t=1 and sum=1.
  2. It assigns the value of i=1 and the loop continues till the condition of the for loop is true.

2.1.   i<=n    (1<=5)    for loop condition is true

t = 1 * 2 / 1

So,  t = 2

sum = 1 + 2

So,  sum = 3

i++

So,  i=2

2.2.   i<=n    (2<=5)    for loop condition is true

t = 2 * 2 / 2

So,  t = 2

sum = 3 + 2

So,  sum = 5

i++

So,  i=3

2.3.   i<=n    (3<=5)    for loop condition is true

t = 2 * 2 / 3

So,  t = 1.3333

sum = 5 + 1.3333

So,  sum=6.3333

i++

So,  i=4

2.4.   i<=n    (4<=5)    for loop condition is true

t = 1.3333 * 2 / 4

So,  t = 0.6667

sum = 6.3333 + 0.6667

So,  sum=7

i++

So,  i=5

2.5.   i<=n    (5<=5)    for loop condition is true

t = 0.6667 * 2 / 5

So,  t = 0.2667

sum = 7 + 0.2667

So,  sum=7.2667

i++

So,  i=6

2.6.   i<=n    (6<=5)    for loop condition is false

It comes out of the for loop.

  1. Finally it prints The Exponential value of 2 = 7.2667
  2. Thus program execution is completed.

Output:

exponential series

TO DOWNLOAD THE PROGRAM CODE : CLICK HERE

 

Leave a Reply

Your email address will not be published. Required fields are marked *