First N Terms of AP

Print first n terms of arithmetic progression (a, d).

IntermediatePhase 2 - Level 3: Mathematical & Logical PatternsExample 9 of 13
first-n-terms-of-ap.py
1# Take inputs
2a = int(input("Enter first term (a): "))
3d = int(input("Enter common difference (d): "))
4n = int(input("Enter number of terms (n): "))
5
6# Print AP
7print(f"First {n} terms of AP:")
8for i in range(n):
9 term = a + i * d
10 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

What's going on

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