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


March 25, 2022, Learn eTutorial
1444

What is Euler's number?

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

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

In this python example, we are using the formula to solve the problem. After accepting the number we initialize the eu_sum as 1 and open the for loop from i=1 to the number of terms n+1 given by the user. Then we use the arithmetic formula eu_sum = eu_sum+(1/math.factorial(i)). Finally, we print the eu_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 factorials very easily.

ALGORITHM

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

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

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

STEP 4: Open a for loop from i=1 to the number of terms n+1 using the range method.

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

STEP 6: Using a print statement we print the result eu_sum using a round for getting precision in decimal numbers.


For solving the Euler's number program in python we have to understand the below topics, we recommend to refer these topics for a better understanding

Python Source Code

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

                                      

OUTPUT

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