This blog is about providing theory as well as simple executable codes of different programming languages such as java, C, C++, and web programming, etc. This blog will be helpful to the IT students to learn about programming.

Wednesday, March 15, 2023

Write a program to find the value of e in the following series: e=1+1/1!+1/2!+......... upto acc = 0.0001

 To find the value of e in the given series, we need to sum up the terms until the accuracy reaches 0.0001. We can use a loop to add the terms and check for the accuracy. Here's the program to find the value of e:

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    double e = 1.0, term = 1.0;
    int i = 1;
    const double acc = 0.0001; // desired accuracy

    while (fabs(term) > acc) {
        term /= i; // i! = i * (i-1)!, so divide by i to get (i-1)!
        e += term;
        i++;
    }

    cout << "The value of e is: " << e << endl;
    return 0;
}

In this program, we initialize the value of e and term to 1.0 and i to 1. We also set the desired accuracy acc to 0.0001.

The loop continues until the absolute value of term is less than or equal to acc. In each iteration, we divide term by i to get the next term in the series and add it to e. We also increment i by 1 in each iteration.

Finally, we print the value of e on the screen. When we run the program, the output will be:

The value of e is: 2.71828

This is the approximate value of the mathematical constant e up to 5 decimal places.

No comments:

Post a Comment

If you have any doubts, please let me know

Slider Widget