In this python program, we will learn how to calculate the sum of the Geometric Progression(G.P.) series. In this program, we calculate the sum of the G.P. series with or without using mathematics formula.
A Geometric Progression series is a sequence of numbers where each term after the first term is found by multiplying the previous one by a fixed, non-zero number called the common ratio. For example, the sequence 1, 3, 9, 27,81,... is a geometric progression with common ratio 3.
Sum of First Nth terms of a GP (Sn) = a1(1−rn)/1−r
where r≠1,
r = common ration
a = First number of the sequence
n = Total number of terms in the G.P. Series
Here is the source code of the program to calculate the sum of the Geometric Progression(G.P.) series.
# Python Program to Calculate the Sum of Geometric Progression Series
# Using math.pow() function
# Import Module
import math
# Take the Input from the User
first_Num = int(input("Enter First Number of an G.P. Series: "))
Total_num = int(input("Enter the Total Numbers in this G.P. Series: "))
ratio = int(input("Enter the Common Difference: "))
# Calculation
total = (first_Num * (1 - math.pow(ratio, Total_num ))) / (1- ratio)
tn = first_Num * (math.pow(ratio, Total_num - 1))
# Print the Output
print("\nThe Sum of Geometric Progression Series = " , total)
print("The tn Term of Geometric Progression Series = " , tn)
Enter First Number of an G.P. Series: 5
Enter the Total Numbers in this G.P. Series: 6
Enter the Common Difference: 3
The Sum of Geometric Progression Series = 1820.0
The tn Term of Geometric Progression Series = 1215.0
Program 2: Python Program to Calculate the Sum of Geometric Progression Series Without using Mathematics Function
# Python Program to Calculate the Sum of Geometric Progression Series
# Without using Mathematics Function
# Take the Input from the User
first_Num = int(input("Enter First Number of an G.P. Series: "))
Total_num = int(input("Enter the Total Numbers in this G.P. Series: "))
ratio = int(input("Enter the Common Difference: "))
total = 0
value = first_Num
GPseries = ""
# Calculation
print("\nGeometric Progression(G.P) Series :", end = " ")
for i in range(Total_num):
GPseries= GPseries + " + " + str(value)
#print("%d +" %value, end = " ")
total = total + value
value = value * ratio
# Print the output
print(GPseries.lstrip(' + '))
print("\nThe Sum of Geometric Progression Series = " , total)
Enter First Number of an G.P. Series: 5
Enter the Total Numbers in this G.P. Series: 6
Enter the Common Difference: 3
Geometric Progression(G.P) Series : 5 + 15 + 45 + 135 + 405 + 1215
The Sum of Geometric Progression Series = 1820
In the Above program 1 and 2, tn is the last term of the Geometric progression series.
Comments