In the previous article, we have discussed Python Program to Find Volume and Surface Area of a Cuboid
Arithmetic progression:
An Arithmetic progression is a mathematical sequence of numbers in which the difference between the consecutive terms is constant.
In general, an arithmetic sequence looks like this: a, a+d, a+2d, a+3d,…………….
where a = first term
d= common difference
n= number of terms in series
Formula : d= second term – first term
The sum of Arithmetic progression Series : Sn = n/2(2a + (n – 1) d)
The Tn (nth term) of Arithmetic progression Series : Tn = a + (n – 1) d
Given a, n, d values and the task is to find the sum of the Arithmetic progression Series.
Examples:
Example 1:
Input:
Given first term = 3 Given total terms = 9 Given common difference = 4
Output:
Given Arithmetic Progression Series Sum with [a,n,d]:( 3 9 4 ) = 171.0 The Given Arithmetic Progression Series nth Term with [a,n,d]:( 3 9 4 ) = 35
Example 2:
Input:
Given first term = 7 Given total terms = 15 Given common difference = 2
Output:
Given Arithmetic Progression Series Sum with [a,n,d]:( 7 15 2 ) = 315.0 The Given Arithmetic Progression Series nth Term with [a,n,d]:( 7 15 2 ) = 35
Program to Find Sum of Arithmetic Progression Series
Below are the ways to find the sum of the Arithmetic progression Series:
Method #1: Using Mathematical Formula (Static Input)
Approach:
- Give the first term of arithmetic progression series as static input and store it in a variable.
- Give the total number of terms of the A.P. series as static input and store it in another variable.
- Give the common difference of the A.P. series as static input and store it in another variable.
- Calculate the sum of the given arithmetic progression series using the above given mathematical formula(n/2(2a + (n – 1) d)) and store it in a variable.
- Calculate the nth term of the given arithmetic progression series using the above given mathematical formula ( Tn = a + (n – 1) d) and store it in another variable.
- Print the sum and nth term of the given Arithmetic Progression series.
- The Exit of the program.
Below is the implementation:
# Give the first term of arithmetic progression series as static input # and store it in a variable. fst_trm = 2 # Give the total number of terms of the A.P. series as static input and # store it in another variable. total_terms = 6 # Give the common difference of the A.P. series as static input and store it # in another variable. common_diff = 4 # Calculate the sum of the given arithmetic progression series using the above given # mathematical formula(n/2(2a + (n – 1) d)) and store it in a variable. sum_ap = (total_terms * (2 * fst_trm + (total_terms - 1) * common_diff)) / 2 # Calculate the nth term of the given arithmetic progression series using the above # given mathematical formula ( Tn = a + (n – 1) d) and store it in another variable. nth_trm_ap = fst_trm + (total_terms - 1) * common_diff # Print the sum and nth term of the given Arithmetic Progression series. print("Given Arithmetic Progression Series Sum with [a,n,d]:(", fst_trm, total_terms, common_diff, ") = ", sum_ap) print("The Given Arithmetic Progression Series nth Term with [a,n,d]:(", fst_trm, total_terms, common_diff, ") = ", nth_trm_ap)
Output:
Given Arithmetic Progression Series Sum with [a,n,d]:( 2 6 4 ) = 72.0 The Given Arithmetic Progression Series nth Term with [a,n,d]:( 2 6 4 ) = 22
Method #2: Using Mathematical Formula (User Input)
Approach:
- Give the first term of arithmetic progression series as user input using the int(input()) function and store it in a variable.
- Give the total number of terms of the A.P. series as user input using the int(input()) function and store it in another variable.
- Give the common difference of the A.P. series as user input using the int(input()) function and store it in another variable.
- Calculate the sum of the given arithmetic progression series using the above given mathematical formula(n/2(2a + (n – 1) d)) and store it in a variable.
- Calculate the nth term of the given arithmetic progression series using the above given mathematical formula ( Tn = a + (n – 1) d) and store it in another variable.
- Print the sum and nth term of the given Arithmetic Progression series.
- The Exit of the program.
Below is the implementation:
# Give the first term of arithmetic progression series as user input using the # int(input()) function and store it in a variable. fst_trm = int(input("Enter some random number = ")) # Give the total number of terms of the A.P. series as user input using the int(input()) function and store # it in another variable. total_terms = int(input("Enter some random number = ")) # Give the common difference of the A.P. series as user input using the int(input()) function and # store it in another variable. common_diff = int(input("Enter some random number = ")) # Calculate the sum of the given arithmetic progression series using the above given # mathematical formula(n/2(2a + (n – 1) d)) and store it in a variable. sum_ap = (total_terms * (2 * fst_trm + (total_terms - 1) * common_diff)) / 2 # Calculate the nth term of the given arithmetic progression series using the above # given mathematical formula ( Tn = a + (n – 1) d) and store it in another variable. nth_trm_ap = fst_trm + (total_terms - 1) * common_diff # Print the sum and nth term of the given Arithmetic Progression series. print("Given Arithmetic Progression Series Sum with [a,n,d]:(", fst_trm, total_terms, common_diff, ") = ", sum_ap) print("The Given Arithmetic Progression Series nth Term with [a,n,d]:(", fst_trm, total_terms, common_diff, ") = ", nth_trm_ap)
Output:
Enter some random number = 3 Enter some random number = 9 Enter some random number = 4 Given Arithmetic Progression Series Sum with [a,n,d]:( 3 9 4 ) = 171.0 The Given Arithmetic Progression Series nth Term with [a,n,d]:( 3 9 4 ) = 35
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.