# Python Program for Geometric Progression

Geometric Progression:

A geometric series is a sequence of elements in which the next item is obtained by multiplying the previous item by the common ratio.

A G.P. Series is a number series in which the common ratio of any consecutive integers (items) is always the same.

Formulas:

The sum of GP ( Sn ) = a(r^n)/(1-r)
Nth term (Tn)  = a* r^(n-1)


where  a = first term

r = common ratio

n= number of terms

Given a, r, n values, and the task is to calculate the geometric progression series.

## Program for Geometric Progression in Python

### Method #1: Using Mathematical Formula (Static Input)

1)Printing the first n terms of Geometric Progression:

Approach:

• Give the first term (a) as static input and store it in a variable.
• Give a common ratio (r) as static input and store it in another variable.
• Give the number of terms(n) as static input and store it in another variable.
• loop from 1 to gvn_n+1 using the for loop(excludes last no).
• Inside the for loop, calculate the nth term of GP using the above-given mathematical formula and store it in a variable.
• Print the above result.
• Print the nth term of GP.
• The Exit of the Program.

Below is the implementation:

# Give the first term (a) as static input and store it in a variable.
gvn_a = 2
# Give the common ratio (r) as static input and store it in another variable.
gvn_r = 3
# Give the number of terms(n) as static input and store it in another variable.
gvn_n = 7
# loop from 1 to gvn_n+1 using the for loop(excludes last no)
for itr in range(1, gvn_n+1):
# Inside the for loop, calculate the nth term of GP using the above given
# mathematical formula and store it in a variable.
nth_term = gvn_a * gvn_r**(itr-1)
# Print the above result
print(nth_term)
# Print the nth term of GP
print("The nth_term of GP = ", nth_term)


Output:

2
6
18
54
162
486
1458
The nth_term of GP =  1458

2)Calculating the sum of GP

Approach:

• Give the first term (a) as static input and store it in a variable.
• Give a common ratio (r) as static input and store it in another variable.
• Give the number of terms(n) as static input and store it in another variable.
• Check if the given common ratio(gvn_r) value is greater than 1 using the for loop.
• If it is true, then calculate the sum of n terms of Gp using the above-given respective mathematical formula and store it in a variable.
• Else calculate the sum of n terms of Gp using the above-given respective mathematical formula and store it in a variable.
• Print the Sum of Geometric Progression(GP) of given n terms.
• The Exit of the Program.

Below is the implementation:

# Give the first term (a) as static input and store it in a variable.
gvn_a = 2
# Give the common ratio (r) as static input and store it in another variable.
gvn_r = 3
# Give the number of terms(n) as static input and store it in another variable.
gvn_n = 7
# Check if the given common ratio(gvn_r) value is greater than 1 using the for loop
if(gvn_r > 1):
# If it is true, then calculate the sum of n terms of Gp using the above given
# respective mathematical formula and store it in a variable.
Sum_Gp = (gvn_a*(gvn_r**gvn_n))/(gvn_r-1)
else:
# Else calculate the sum of n terms of Gp using the above given
# respective mathematical formula and store it in a variable.
Sum_Gp = (gvn_a*(gvn_r**gvn_n))/(1-gvn_r)
# Print the Sum of Geometric Progression(GP) of given n terms.
print("The Sum of Geometric Progression(GP) = ", Sum_Gp)


Output:

The Sum of Geometric Progression(GP) =  2187.0

### Method #2: Using Mathematical Formula (User Input)

1)Printing the first n terms of Geometric Progression:

Approach:

• Give the first term (a) as user input using the int(input()) function and store it in a variable.
• Give a common ratio (r) as user input using the int(input()) function and store it in another variable.
• Give the number of terms(n) as user input using the int(input()) function and store it in another variable.
• loop from 1 to gvn_n+1 using the for loop(excludes last no).
• Inside the for loop, calculate the nth term of GP using the above-given mathematical formula and store it in a variable.
• Print the above result.
• Print the nth term of GP.
• The Exit of the Program.

Below is the implementation:

# Give the first term (a) as user input using the int(input()) function and store it in a variable.
gvn_a = int(input("Enter some random number = "))
# Give the common ratio (r) as user input using the int(input()) function
# and store it in another variable.
gvn_r = int(input("Enter some random number = "))
# Give the number of terms(n) as user input using the int(input()) function and
# store it in another variable.
gvn_n = int(input("Enter some random number = "))

# loop from 1 to gvn_n+1 using the for loop(excludes last no)
for itr in range(1, gvn_n+1):
# Inside the for loop, calculate the nth term of GP using the above given
# mathematical formula and store it in a variable.
nth_term = gvn_a * gvn_r**(itr-1)
# Print the above result
print(nth_term)
# Print the nth term of GP
print("The nth_term of GP = ", nth_term)

Output:

Enter some random number = 1
Enter some random number = 2
Enter some random number = 6
1
2
4
8
16
32
The nth_term of GP = 32

2)Calculating the sum of GP

Approach:

• Give the first term (a) as user input using the int(input()) function and store it in a variable.
• Give a common ratio (r) as user input using the int(input()) function and store it in another variable.
• Give the number of terms(n) as user input using the int(input()) function and store it in another variable.
• Check if the given common ratio(gvn_r) value is greater than 1 using the for loop.
• If it is true, then calculate the sum of n terms of Gp using the above-given respective mathematical formula and store it in a variable.
• Else calculate the sum of n terms of Gp using the above-given respective mathematical formula and store it in a variable.
• Print the Sum of Geometric Progression(GP) of given n terms.
• The Exit of the Program.

Below is the implementation:

# Give the first term (a) as user input using the int(input()) function and store it in a variable.
gvn_a = int(input("Enter some random number = "))
# Give the common ratio (r) as user input using the int(input()) function
# and store it in another variable.
gvn_r = int(input("Enter some random number = "))
# Give the number of terms(n) as user input using the int(input()) function and
# store it in another variable.
gvn_n = int(input("Enter some random number = "))

# Check if the given common ratio(gvn_r) value is greater than 1 using the for loop
if(gvn_r > 1):
# If it is true, then calculate the sum of n terms of Gp using the above given
# respective mathematical formula and store it in a variable.
Sum_Gp = (gvn_a*(gvn_r**gvn_n))/(gvn_r-1)
else:
# Else calculate the sum of n terms of Gp using the above given
# respective mathematical formula and store it in a variable.
Sum_Gp = (gvn_a*(gvn_r**gvn_n))/(1-gvn_r)
# Print the Sum of Geometric Progression(GP) of given n terms.
print("The Sum of Geometric Progression(GP) = ", Sum_Gp)

Output:

Enter some random number = 1
Enter some random number = 2
Enter some random number = 6
The Sum of Geometric Progression(GP) = 64.0