First N Terms of GP
Print first n terms of geometric progression (a, r).
IntermediatePhase 2 - Level 3: Mathematical & Logical PatternsExample 10 of 13
first-n-terms-of-gp.py
1# Take inputs2a = int(input("Enter first term (a): "))3r = int(input("Enter common ratio (r): "))4n = int(input("Enter number of terms (n): "))56# Print GP7print(f"First {n} terms of GP:")8for i in range(n):9 term = a * (r ** i)10 print(term, end=" ")11print()
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
What's going on
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
🔗Related Content
- 📖
Phase 2 - Learn Concepts
Review Phase 2 concepts and explanations
- 📝
Phase 2 - Quiz
Test your Phase 2 understanding with quiz questions
- 💻
Phase 2 - All Practice Problems
Explore all practice problems for Phase 2
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum