Input x and n, Print Sum of Series

AIM

To write Python programs to input the value of x and n and print the sum of different mathematical series.

ALGORITHM

  1. Start
  2. Input the value of x (base) and n (number of terms)
  3. Initialize sum variable according to the series type
  4. Use a loop to calculate each term of the series
  5. Add or subtract terms based on the series pattern
  6. Display the final sum
  7. Stop

PROGRAM

1. Series: 1 + x + x² + x³ + ... + xⁿ

# Series 1: 1 + x + x^2 + x^3 + ... + x^n
x = float(input('Enter value of x: '))
n = int(input('Enter value of n: '))
s = 0
for i in range(n + 1):
    s += x**i
print('Sum of the series:', s)

2. Series: 1 - x + x² - x³ + ... ± xⁿ

# Series 2: 1 - x + x^2 - x^3 + ... +/- x^n
x = float(input('Enter value of x: '))
n = int(input('Enter value of n: '))
s = 1
for i in range(1, n+1):
    if i%2 == 0:
        s = s + x**i
    else:
        s = s - x**i
print('Sum of the series:', s)

3. Series: x - x²/2 + x³/3 - x⁴/4 + ...

# Series 3: x - x^2/2 + x^3/3 - x^4/4 + ...
x = float(input('Enter value of x: '))
n = int(input('Enter value of n: '))
sum = x
for i in range(2, n+1):
    if (i%2 == 0):
        sum -= (x**i)/i
    else:
        sum += (x**i)/i
print('Sum is:', sum)

4. Series: x - x²/2! + x³/3! - x⁴/4! + ...

# Series 4: x - x^2/2! + x^3/3! - x^4/4! + ...
import math
x = float(input('Enter value of x: '))
n = int(input('Enter value of n: '))
sum = x
for i in range(2, n+1):
    if (i%2 == 0):
        sum = sum + ((x**i)/math.factorial(i))
    else:
        sum = sum - ((x**i)/math.factorial(i))
print('Sum is:', sum)

OUTPUT

Series 1 Output:

Enter value of x: 2

Enter value of n: 4

Sum of the series: 31.0

Series 2 Output:

Enter value of x: 2

Enter value of n: 4

Sum of the series: 11.0

Series 3 Output:

Enter value of x: 2

Enter value of n: 4

Sum is: 0.6666666666666667

Series 4 Output:

Enter value of x: 2

Enter value of n: 4

Sum is: 1.4666666666666666

CONCLUSION

Thus, the given program was successfully executed and the output was verified as per the expected result.

VIVA QUESTIONS

  1. What is the purpose of these series programs?

    These programs calculate the sum of different mathematical series like geometric series, alternating series, and factorial-based series which are commonly used in mathematics and engineering.

  2. What is the difference between Series 1 and Series 2?

    Series 1 is a simple geometric series where all terms are positive. Series 2 is an alternating series where terms alternate between positive and negative based on whether the power is even or odd.

  3. Why do we use math.factorial() in Series 4?

    Series 4 involves factorial terms in the denominator (2!, 3!, 4!, etc.). The math.factorial() function calculates the factorial of a number efficiently.

  4. What happens if we input negative values for x?

    Negative values for x are valid and will produce different results. For alternating series, negative x values can affect the sign pattern of the terms.

  5. How does the modulo operator (%) work in these programs?

    The modulo operator (%) returns the remainder after division. We use i%2 to check if a number is even (remainder 0) or odd (remainder 1) to determine the sign of terms in alternating series.

  6. What is the mathematical significance of Series 4?

    Series 4 represents the Taylor series expansion of sin(x) or cos(x) functions, which are fundamental in calculus and used to approximate trigonometric functions.

  7. Why do we initialize 'sum' differently in different series?

    The initialization depends on the first term of each series. Series 1 and 2 start with 1, while Series 3 and 4 start with x, so we initialize accordingly.

  8. What would happen if we input a very large value for n?

    For large n values, the computation time increases, and for Series 4, factorial values become very large, which might cause overflow errors or precision issues in floating-point calculations.

Related Resources

Mathematical Concepts

  • • Geometric progression and series
  • • Alternating series convergence
  • • Taylor series expansions
  • • Factorial calculations
  • • Power functions and exponents

Programming Tips

  • • Use appropriate data types (float/int)
  • • Handle large factorial values carefully
  • • Test with different x and n values
  • • Understand alternating sign patterns
  • • Import math module for factorial

Need Help?

Join our tuition classes for detailed explanation of mathematical series and programming concepts.

Register for Classes →