Logic Building
# Take inputs
a = int(input("Enter first term (a): "))
d = int(input("Enter common difference (d): "))
n = int(input("Enter number of terms (n): "))
# Print AP
print(f"First {n} terms of AP:")
for i in range(n):
term = a + i * d
print(term, end=" ")
print()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
AP: each term = first term + (position - 1) * difference.
Key Concepts:
- nth term = a + (n-1) * d
- Loop from 0 to n-1
- Calculate each term