First N Terms of AP
Generate arithmetic-progression terms from a starting value and difference. Students use zero-based range positions and see how each position sets the term.
1# Take inputs2a = int(input("Enter first term (a): "))3d = int(input("Enter common difference (d): "))4n = int(input("Enter number of terms (n): "))56# Print AP7print(f"First {n} terms of AP:")8for i in range(n):9 term = a + i * d10 print(term, end=" ")11print()
Output
Enter first term (a): 2 Enter common difference (d): 3 Enter number of terms (n): 5 First 5 terms of AP: 2 5 8 11 14
In this one
- Use a common difference
- Index terms from zero
- Generate a fixed term count
How it works
- 1
Read a, d, and n.
- 2
Visit positions zero through n - 1.
- 3
Print a + i * d for each position.
Edge cases
Can d be negative?
Can d be negative?
Yes. The terms then decrease.
What does n = 0 do?
What does n = 0 do?
It prints the heading but no terms.
What's going on
The term at position i is a + i * d. Since range(n) starts at zero, the first printed term is a, not a + d.
Each successive position adds one more copy of the common difference. With a equal to two and d equal to three, the beginning is 2, 5, 8. n controls the number of terms, not the largest allowed value.
Can d be negative? Yes. The terms then decrease.
What does n = 0 do? It prints the heading but no terms.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Why start i at zero?
Pick an answer to check it.
Question 2
Can d be negative?
Pick an answer to check it.
Questions people ask
Can d be negative?
Yes. The terms then decrease.
What does n = 0 do?
It prints the heading but no terms.
Why start i at zero?
The first term should equal a.
๐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