Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.
Given a number numb, the task is to compute the value of the Euler’s number expansion in Python.
Examples:
Example1:
Input:
given number =3
Output:
Enter some random number = 3 The total sum of euler's value of the series 2.67
Example2:
Input:
given number =5
Output:
The total sum of euler's value of the series 2.72
Program to Compute the Value of Euler’s Number ,Using the Formula: e = 1 + 1/1! + 1/2! + …… 1/n! in Python
There are several ways to calculate the value of the euler’s number some of them are:
- Using factorial function and for loop(Static Input)
- Using factorial function and for loop(User Input)
- Using factorial function and while loop(Static Input)
- Using factorial function and while loop(User Input)
Method #1:Using factorial function and for loop(Static Input)
Approach:
- Give the number as static input.
- Set the value of the totalsum variable to 1.
- Find the total of the series using a for loop ranging from 1 to the number.
- Compute the factorial using math. factorial() function.
- After rounding to two decimal places, print the totalsum of the series.
- Exit of program.
Below is the implementation:
import math # Give the number of terms as static input. numb = 5 # Set a totalsum variable which calculates the total sum and initialize it to 1. totalsum = 1 # Find the total of the series using a for loop ranging from 1 to the number. for val in range(1, numb+1): # Compute the factorial using math. factorial() function. totalsum = totalsum+(1/math.factorial(val)) # Print the totalsum value of the euler's series, rounded to two decimal places. print("The total sum of euler's value of the series ", round(totalsum, 2))
Output:
The total sum of euler's value of the series 2.72
Method #2:Using factorial function and for loop(User Input)
Approach:
- Scan the given number as user input by using int(input()) function.
- Set the value of the totalsum variable to 1.
- Find the total of the series using a for loop ranging from 1 to the number.
- Compute the factorial using math. factorial() function.
- After rounding to two decimal places, print the totalsum of the series.
- Exit of program.
Below is the implementation:
import math # Scan the number as user input by using int(input()) function numb = int(input('Enter some random number = ')) # Set a totalsum variable which calculates the total sum and initialize it to 1. totalsum = 1 # Find the total of the series using a for loop ranging from 1 to the number. for val in range(1, numb+1): # Compute the factorial using math. factorial() function. totalsum = totalsum+(1/math.factorial(val)) # Print the totalsum value of the euler's series, rounded to two decimal places. print("The total sum of euler's value of the series ", round(totalsum, 2))
Output:
Enter some random number = 13 The total sum of euler's value of the series 2.72
Method #3:Using factorial function and while loop(Static Input)
Approach:
- Give the number as static input.
- Set a totalsum variable which calculates the total sum and initialize it to 0.
- Take a variable say value and initialize it to 1.
- Using while loop calculate the total sum till value greater than given number
- Compute the factorial using math. factorial() function.
- Calculate the totalsum by incrementing it value with 1/factorial value.
- Increment the value by 1.
- Print the totalsum of the series, rounded to two decimal places.
- Exit of program.
Below is the implementation:
import math # Give the number of terms as static input. numb = 3 # Take a variable say value and initialize it to 1. value = 1 # Set a totalsum variable which calculates the total sum and initialize it to 1. totalsum = 1 # Using while loop calculate the total sum till value greater than given number while(value <= numb): # Compute the factorial using math. factorial() function. totalsum = totalsum+(1/math.factorial(value)) # Increment the value by 1. value = value+1 # Print the totalsum value of the euler's series, rounded to two decimal places. print("The total sum of euler's value of the series ", round(totalsum, 2))
Output:
The total sum of euler's value of the series 2.67
Method #4:Using factorial function and while loop(User Input)
Approach:
- Scan the given number as user input by using int(input()) function.
- Set a totalsum variable which calculates the total sum and initialize it to 0.
- Take a variable say value and initialize it to 1.
- Using while loop calculate the total sum till value greater than given number
- Compute the factorial using math. factorial() function.
- Calculate the totalsum by incrementing it value with 1/factorial value.
- Increment the value by 1.
- Print the totalsum of the series, rounded to two decimal places.
- Exit of program.
Below is the implementation:
import math # Scan the number as user input by using int(input()) function numb = int(input('Enter some random number = ')) # Take a variable say value and initialize it to 1. value = 1 # Set a totalsum variable which calculates the total sum and initialize it to 1. totalsum = 1 # Using while loop calculate the total sum till value greater than given number while(value <= numb): # Compute the factorial using math. factorial() function. totalsum = totalsum+(1/math.factorial(value)) # Increment the value by 1. value = value+1 # Print the totalsum value of the euler's series, rounded to two decimal places. print("The total sum of euler's value of the series ", round(totalsum, 2))
Output:
Enter some random number = 3 The total sum of euler's value of the series 2.67
Related Programs: