This Python program reads two numbers from the user: x and n.
It calculates the sum of the exponential series approximation:
x¹/1! + x²/2! + x³/3! + ... + xⁿ/n!
This series is used to approximate the value of eˣ using n terms.
The program uses a factorial method and a loop to compute each term.
def factorial(num: int) -> int:
result = 1
for i in range(2, num + 1):
result *= i
return result
def exponential_series(x: float, n: int) -> float:
total = 0.0
for i in range(1, n + 1):
total += (x ** i) / factorial(i)
return total
# Read inputs from user
x = float(input("Enter value for x: "))
n = int(input("Enter number of terms n: "))
result = exponential_series(x, n)
print(f"The result of the exponential series is: {result:.6f}")
The result of the exponential series is: 5.000000
- The user enters values for x and n
- The factorial method computes each denominator
- The exponential_series method calculates each term and adds it to the total
- The final result is printed with six decimal places for precision