Logic Building
# Take inputs
a = int(input("Enter first term (a): "))
r = int(input("Enter common ratio (r): "))
n = int(input("Enter number of terms (n): "))
# Print GP
print(f"First {n} terms of GP:")
for i in range(n):
term = a * (r ** i)
print(term, end=" ")
print()Output
Enter first term (a): 2 Enter common ratio (r): 3 Enter number of terms (n): 5 First 5 terms of GP: 2 6 18 54 162
GP: each term = first term * (ratio ^ (position - 1)).
Key Concepts:
- nth term = a * (r ^ (n-1))
- Loop from 0 to n-1
- Calculate each term using power