Python Program to compute the value of euler’s number e

In this simple python program, we need to compute the Euler's number. It is a number-based python program.

For a better understanding of this example, we always recommend you to learn the basic topics of Python programming listed below:

What is Euler's number?

In this python program, we have to calculate the value of Euler’s number using the formula 1 + 1 / 1! + 1 / 2!... For that, we have to know about Euler's number. Euler’s number is a sum of infinite series, it is a mathematical constant approximately equal to 2.718. Euler number is the base of the logarithm table and it is used in calculating the compound interest.

How to find the value of Euler's number in python?

In this python example, we are using the formula to solve the problem so we are checking how we implement the formula in the python programming language. After accepting the number we initialize the sum as 1 and open the for loop in python from one to the number of terms given by the user. Then we use the arithmetic formula 1 + 1/1! + 1/2! sum1+(1/math.factorial(i)). Finally, we print the sum of the series using the print and round method to get 2 precision decimal in the result. In this python program, a new math module is used to find factorial very easily.

ALGORITHM

STEP 1: Import a math module to calculate the mathematical operations easily.

STEP 2: Accept the number of terms from the user using the input function and convert the string to an integer using int data type in python language.

STEP 3: Initialize the sum variable to apply Euler's formula.

STEP 4: Open a for loop from 1 to the number of terms using the range method in python.

STEP 5: Calculate the sum using the formula sum = sum + 1 / factorial of i using math module in the python programming language.

STEP 6: Using a print statement in python basics we print the resulting sum using a round for getting precision in decimal numbers.

Python Source Code

                                          import math
n=int(input("Enter the number of terms: "))
sum1=1
for i in range(1,n+1):
    sum1=sum1+(1/math.factorial(i))
print("The sum of series is",round(sum1,2))

                                      

OUTPUT

Enter the number of terms: 5
The sum of series is 2.72