Python Program to find the Sum of Arithmetic Progression

In this tutorial, we will discuss a Python program to find the sum of an arithmetic progression (AP).

Before going to the program first, let us understand what is an Arithmetic Progression(AP).

Arithmetic Progression (AP):

  • An arithmetic progression is a sequence of numbers in which the difference between consecutive terms is constant.
  • The sum of the first nn terms of an AP is given by the formula: Sn=n2(2a+(n−1)d)S_n = \frac{n}{2} \left(2a + (n – 1)d\right), where aa is the first term and dd is a common difference.

Related:  Python Program to find the Sum of Geometric Progression

Program code for Arithmetic Progression in Python

# Sum of an Arithmetic Progression in Python
def sum_of_ap(a, d, n):
    return (n / 2) * (2 * a + (n - 1) * d)

a = float(input("Enter the first term (a): "))
d = float(input("Enter the common difference (d): "))
n = int(input("Enter the number of terms (n): "))

sum_ap = sum_of_ap(a, d, n)
print(f"The sum of the first {n} terms of the AP is {sum_ap}.")

Explanation

  1. Function Definition: The sum_of_ap function takes three parameters: a, d, and n, and returns the sum of the first nn terms of the AP.
  2. Main Program: The program prompts the user to enter the first term, common difference, and number of terms. It then calculates the sum of the AP using the sum_of_ap function and prints the result.

Output

Python Program to find the Sum of Arithmetic Progression

  • When you run the above program, it will prompt you to enter the first term, common difference, and number of terms.
  • After entering the values, it will calculate the sum of the AP and print the result.

Conclusion

  • In this tutorial, we learned how to find the sum of an arithmetic progression (AP) using a Python program.
  • Understanding this concept is essential for solving various mathematical problems and enhancing your programming skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *