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:
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!
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.
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 eu_sum as 1 and open the
for loop in python 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
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.
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 in python language.
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 in python.
STEP 5: Calculate the sum using the formula eu_sum = eu_sum + 1 / factorial of i using
math module in the python programming language.
STEP 6: Using a
round for getting precision in decimal numbers.
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))
Enter the number of terms: 5 The sum of series is 2.72