Python Program for Arithmetic Progression

Arithmetic Progression:

The Arithmetic Series is a term series in which the next item is formed by adding a common difference to the previous item.

The A.P. series is a numerical sequence in which the difference between any two consecutive numbers is always the same. This is referred to as a common difference.

Formulas:

The sum of AP ( Sn ) =  n/2(2a + (n – 1) d)
Nth term (Tn)  = a + (n – 1) d

where  a = first term

d = common difference

n= number of terms

Given a, d, n values, and the task is to calculate the Arithmetic progression series.

Program for Arithmetic Progression in Python

Method #1: Using Mathematical Formula (Static Input)

1)Printing the first n terms of Arithmetic Progression:

Approach:

  • Give the first term (a) as static input and store it in a variable.
  • Give a common difference (d) as static input and store it in another variable.
  • Give the number of terms(n) as static input and store it in another variable.
  • loop from 1 to gvn_n+1 using the for loop(excludes last no).
  • Inside the for loop, calculate the nth term of AP using the above-given mathematical formula and store it in a variable.
  • Print the above result.
  • Print the nth term of AP.
  • The Exit of the Program.

Below is the implementation:

# Give the first term (a) as static input and store it in a variable.
gvn_a = 2
# Give the common difference (d) as static input and store it in another variable.
gvn_d = 3
# Give the number of terms(n) as static input and store it in another variable.
gvn_n = 7
# loop from 1 to gvn_n+1 using the for loop(excludes last no)
for itr in range(1, gvn_n+1):
    # Inside the for loop, calculate the nth term of AP using the above given
    # mathematical formula and store it in a variable.
    nth_term = gvn_a + (itr-1)*gvn_d
    # Print the above result
    print(nth_term)
# Print the nth term of AP
print("The nth_term of AP = ", nth_term)

Output:

2
5
8
11
14
17
20
The nth_term of AP =  20

2)Calculating the sum of AP

Approach:

  • Give the first term (a) as static input and store it in a variable.
  • Give a common difference (d) as static input and store it in another variable.
  • Give the number of terms(n) as static input and store it in another variable.
  • Calculate the sum of n terms of AP using the above given mathematical formula and store it in a variable.
  • Print the Sum of Arithmetic Progression(AP) of given n terms.
  • The Exit of the Program.

Below is the implementation:

# Give the first term (a) as static input and store it in a variable.
gvn_a = 2
# Give the common diiference (d) as static input and store it in another variable.
gvn_d = 3
# Give the number of terms(n) as static input and store it in another variable.
gvn_n = 7
# Calculate the sum of n terms of AP using the above given mathematical formula
# and store it in a variable.
Sum_Ap = (gvn_n/2)*(2*gvn_a + (gvn_n-1)*gvn_d)
# Print the Sum of Arithmetic Progression(AP) of given n terms.
print("The Sum of Arithmetic Progression(AP) of given n terms = ", Sum_Ap)

Output:

The Sum of Arithmetic Progression(AP) of given n terms =  77.0

Method #2: Using Mathematical Formula (User Input)

1)Printing the first n terms of Arithmetic Progression:

Approach:

  • Give the first term (a) as user input using the int(input()) function and store it in a variable.
  • Give a common difference (d) as user input using the int(input()) function and store it in another variable.
  • Give the number of terms(n) as user input using the int(input()) function and store it in another variable.
  • loop from 1 to gvn_n+1 using the for loop(excludes last no).
  • Inside the for loop, calculate the nth term of AP using the above-given mathematical formula and store it in a variable.
  • Print the above result.
  • Print the nth term of AP.
  • The Exit of the Program.

Below is the implementation:

# Give the first term (a) as user input using the int(input()) function and store it in a variable.
gvn_a = int(input("Enter some random number = "))
# Give the common difference (d) as user input using the int(input()) function and
# store it in another variable.
gvn_d = int(input("Enter some random number = "))
# Give the number of terms(n) as user input using the int(input()) function and 
# store it in another variable.
gvn_n = int(input("Enter some random number = "))
# loop from 1 to gvn_n+1 using the for loop(excludes last no)
for itr in range(1, gvn_n+1):
    # Inside the for loop, calculate the nth term of AP using the above given
    # mathematical formula and store it in a variable.
    nth_term = gvn_a + (itr-1)*gvn_d
    # Print the above result
    print(nth_term)
# Print the nth term of AP
print("The nth_term of AP = ", nth_term)

Output:

Enter some random number = 1
Enter some random number = 2
Enter some random number = 12
1
3
5
7
9
11
13
15
17
19
21
23
The nth_term of AP = 23

2)Calculating the sum of AP

Approach:

  • Give the first term (a) as user input using the int(input()) function and store it in a variable.
  • Give a common difference (d) as user input using the int(input()) function and store it in another variable.
  • Give the number of terms(n) as user input using the int(input()) function and store it in another variable.
  • Calculate the sum of n terms of AP using the above given mathematical formula and store it in a variable.
  • Print the Sum of Arithmetic Progression(AP) of given n terms.
  • The Exit of the Program.

Below is the implementation:

# Give the first term (a) as user input using the int(input()) function and store it in a variable.
gvn_a = int(input("Enter some random number = "))
# Give the common difference (d) as user input using the int(input()) function and
# store it in another variable.
gvn_d = int(input("Enter some random number = "))
# Give the number of terms(n) as user input using the int(input()) function and 
# store it in another variable.
gvn_n = int(input("Enter some random number = "))
# Calulate the sum of nterms of AP using the above given mathematical formula
# and store it in a variable.
Sum_Ap = (gvn_n/2)*(2*gvn_a + (gvn_n-1)*gvn_d)
# Print the Sum of Arithmetic Progression(AP) of given n terms.
print("The Sum of Arithmetic Progression(AP) of given n terms = ", Sum_Ap)

Output:

Enter some random number = 1
Enter some random number = 2
Enter some random number = 12
The Sum of Arithmetic Progression(AP) of given n terms = 144.0

 

Leave a Comment