Python Program to find the Sum of Geometric Progression
In this tutorial, we will discuss a Python program to find the sum of a geometric progression (GP) .
Before going to the program first, let us understand what is a Geometric Progression(GP).
Geometric Progression(GP):
- A geometric progression is a sequence of numbers where each term after the first is found by multiplying the previous term by a constant called the common ratio.
- The sum of the first nn terms of a GP is given by the formula: Sn=a(1−rn1−r)S_n = a \left(\frac{1 – r^n}{1 – r}\right) for r≠1r \neq 1, where aa is the first term and rr is the common ratio.
Related: Python Program for Simple Calculator
Program code for Geometric Progression in Python
# Sum of a Geometric Progression in Python
def sum_of_gp(a, r, n):
if r == 1:
return a * n
else:
return a * (1 - r ** n) / (1 - r)
a = float(input("Enter the first term (a): "))
r = float(input("Enter the common ratio (r): "))
n = int(input("Enter the number of terms (n): "))
sum_gp = sum_of_gp(a, r, n)
print(f"The sum of the first {n} terms of the GP is {sum_gp}.")
Explanation
- Function Definition: The
sum_of_gpfunction takes three parameters:a,r, andn, and returns the sum of the first nn terms of the GP. - Main Program: The program prompts the user to enter the first term, common ratio, and number of terms. It then calculates the sum of the GP using the
sum_of_gpfunction and prints the result.
Output
- When you run the above program, it will prompt you to enter the first term, common ratio, and number of terms.
- After entering the values, it will calculate the sum of the GP and print the result.
Conclusion
- In this tutorial, we learned how to find the sum of a geometric progression (GP) using a Python program.
- Understanding this concept is essential for solving various mathematical problems and enhancing your programming skills.

