Fibonacci Series Up to N
Print Fibonacci series up to n terms.
IntermediatePhase 2 - Level 2: Number-based Looping LogicExample 9 of 14
fibonacci-series-up-to-n.py
1# Take n2n = int(input("Enter number of terms: "))34# Generate Fibonacci5if n <= 0:6 print("Invalid input")7elif n == 1:8 print("0")9elif n == 2:10 print("0 1")11else:12 a, b = 0, 113 print(a, b, end=" ")14 for i in range(2, n):15 c = a + b16 print(c, end=" ")17 a, b = b, c18 print()
Output
Enter number of terms: 8 0 1 1 2 3 5 8 13
What's going on
Each term is sum of previous two.
Key Concepts:
Start with 0 and 1
Each next term = previous + previous-1
Use two variables to track last two terms
🔗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