Arithmetic progression in Python
In this post, you will learn how to write the Arithmetic progression program using the Python programming language.
An arithmetic progression or arithmetic sequence is a sequence of numbers such that the difference between the consecutive terms is constant. For instance, the sequence 5, 7, 9, 11, 13, 15.. . is an arithmetic progression with a common difference of 2. If we are given the first term a and the common difference d, we can write the second term as a+d, the third term as a+2d, the fourth term as a+3d, and so on.
Arithmetic Progression Formula
For the first term, 'a' of an AP and the common difference 'd', given below is a list of arithmetic progression formulas that are commonly used to solve various problems related to AP.
nth term of an AP: an = a + (n - 1)d
Python Arithmetic Progression Program
def display_arithmetic_progression(a, d, n):
current_value = a
for i in range(0, n):
print(current_value, end=' ')
current_value = current_value + d
x = int(input('Enter the start number: '))
d = int(input('Enter the common difference: '))
n = int(input('Enter total numbers to print: '))
display_arithmetic_progression(x, d, n)
Output of the above code:
Enter the start number:
5
Enter the common difference:
3
Enter total numbers to print:
10
5 8 11 14 17 20 23 26 29 32
Enter the start number:
10
Enter the common difference:
7
Enter total numbers to print:
7
10 17 24 31 38 45 52
Related Articles
Count consonants in a string Python
Python split strings by comma
Count vowels in a string Python
Replace multiple characters Python
Convert array to list Python
Python loop through list
Difference between tuple and list in Python
Convert string to list Python
Remove last element from list Python
Convert list to string Python
Convert Python list to numpy array
Remove element from list Python
Python dict inside list
Check if list is empty Python
Python iterate list with index
Python iterate list with index
Python split string by comma
Printing Simple Diamond Pattern in Python
Stemming and Lemmatization in Python
Python | Generate QR Code using pyqrcode module