Given Mth and Nth term of an arithmetic progression. The task is to find the sum of its first p terms.

**Examples:**

Input:m = 6, n = 10, mth = 12, nth = 20, p = 5

Output:30

Input:m = 10, n = 20, mth = 70, nth = 140, p = 4

Output:70

**Approach:** Let a is the first term and d is the common difference of the given AP. Therefore

mth term = a + (m-1)d and nth term = a + (n-1)d

From these two equations, find the value of a and d. Now use the formula of sum of p terms of an AP.

Sum of p terms =

( p * ( 2*a + (p-1) * d ) ) / 2;

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate the value of the ` `pair<` `double` `, ` `double` `> findingValues(` `double` `m, ` ` ` `double` `n, ` `double` `mth, ` `double` `nth) ` `{ ` ` ` `// Calculate value of d using formula ` ` ` `double` `d = (` `abs` `(mth - nth)) / ` `abs` `((m - 1) - (n - 1)); ` ` ` ` ` `// Calculate value of a using formula ` ` ` `double` `a = mth - ((m - 1) * d); ` ` ` ` ` `// Return pair ` ` ` `return` `make_pair(a, d); ` `} ` ` ` `// Function to calculate value sum ` `// of first p numbers of the series ` `double` `findSum(` `int` `m, ` `int` `n, ` `int` `mth, ` `int` `nth, ` `int` `p) ` `{ ` ` ` ` ` `pair<` `double` `, ` `double` `> ad; ` ` ` ` ` `// First calculate value of a and d ` ` ` `ad = findingValues(m, n, mth, nth); ` ` ` ` ` `double` `a = ad.first, d = ad.second; ` ` ` ` ` `// Calculate the sum by using formula ` ` ` `double` `sum = (p * (2 * a + (p - 1) * d)) / 2; ` ` ` ` ` `// Return the sum ` ` ` `return` `sum; ` `} ` ` ` `// Driven Code ` `int` `main() ` `{ ` ` ` ` ` `double` `m = 6, n = 10, mTerm = 12, nTerm = 20, p = 5; ` ` ` ` ` `cout << findSum(m, n, mTerm, nTerm, p) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the above approach ` `import` `math as mt ` ` ` `# Function to calculate the value of the ` `def` `findingValues(m, n, mth, nth): ` ` ` ` ` `# Calculate value of d using formula ` ` ` `d ` `=` `((` `abs` `(mth ` `-` `nth)) ` `/` ` ` `abs` `((m ` `-` `1` `) ` `-` `(n ` `-` `1` `))) ` ` ` ` ` `# Calculate value of a using formula ` ` ` `a ` `=` `mth ` `-` `((m ` `-` `1` `) ` `*` `d) ` ` ` ` ` `# Return pair ` ` ` `return` `a, d ` ` ` `# Function to calculate value sum ` `# of first p numbers of the series ` `def` `findSum(m, n, mth, nth, p): ` ` ` ` ` `# First calculate value of a and d ` ` ` `a,d ` `=` `findingValues(m, n, mth, nth) ` ` ` ` ` `# Calculate the sum by using formula ` ` ` `Sum` `=` `(p ` `*` `(` `2` `*` `a ` `+` `(p ` `-` `1` `) ` `*` `d)) ` `/` `2` ` ` ` ` `# Return the Sum ` ` ` `return` `Sum` ` ` `# Driver Code ` `m ` `=` `6` `n ` `=` `10` `mTerm ` `=` `12` `nTerm ` `=` `20` `p ` `=` `5` ` ` `print` `(findSum(m, n, mTerm, nTerm, p)) ` ` ` `# This code is contributed by ` `# Mohit Kumar 29 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the above approach ` ` ` `// Function to calculate the value of the ` `function` `findingValues(` `$m` `, ` `$n` `, ` `$mth` `, ` `$nth` `) ` `{ ` ` ` `// Calculate value of d using formula ` ` ` `$d` `= (` `abs` `(` `$mth` `- ` `$nth` `)) / ` ` ` `abs` `((` `$m` `- 1) - (` `$n` `- 1)); ` ` ` ` ` `// Calculate value of a using formula ` ` ` `$a` `= ` `$mth` `- ((` `$m` `- 1) * ` `$d` `); ` ` ` ` ` `// Return pair ` ` ` `return` `array` `(` `$a` `, ` `$d` `); ` `} ` ` ` `// Function to calculate value sum ` `// of first p numbers of the series ` `function` `findSum(` `$m` `, ` `$n` `, ` `$mth` `, ` `$nth` `, ` `$p` `) ` `{ ` ` ` `// First calculate value of a and d ` ` ` `$ad` `= findingValues(` `$m` `, ` `$n` `, ` `$mth` `, ` `$nth` `); ` ` ` ` ` `$a` `= ` `$ad` `[0]; ` ` ` `$d` `= ` `$ad` `[1]; ` ` ` ` ` `// Calculate the sum by using formula ` ` ` `$sum` `= (` `$p` `* (2 * ` `$a` `+ (` `$p` `- 1) * ` `$d` `)) / 2; ` ` ` ` ` `// Return the sum ` ` ` `return` `$sum` `; ` `} ` ` ` `// Driver Code ` `$m` `= 6; ` `$n` `= 10; ` `$mTerm` `= 12; ` `$nTerm` `= 20; ` `$p` `= 5; ` ` ` `echo` `findSum(` `$m` `, ` `$n` `, ` `$mTerm` `, ` `$nTerm` `, ` `$p` `); ` ` ` `// This code is contributed by Ryuga ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

30

## Recommended Posts:

- Find n terms of Fibonacci type series with given first two terms
- Sum of the series 5+55+555+.. up to n terms
- Sum of first n terms of a given series 3, 6, 11, .....
- Sum of the first N terms of the series 2, 6, 12, 20, 30....
- Sum of the first N terms of the series 2,10, 30, 68,....
- Sum of the first N terms of the series 5,12, 23, 38....
- Find the sum of n terms of the series 1,8,27,64 ....
- Find the sum of first N terms of the series 2×3 + 4×4 + 6×5 + 8×6 + ...
- Sum of first N terms of Quadratic Sequence 3 + 7 + 13 + ...
- Find the sum of first N terms of the series 2*3*5, 3*5*7, 4*7*9, ...
- Find Pth term of a GP if Mth and Nth terms are given
- Sum of the series 0.7, 0.77, 0.777, ... upto n terms
- Sum of the series 0.6, 0.06, 0.006, 0.0006, ...to n terms
- Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + - - - - - - upto n terms.
- Sum of series 2/3 - 4/5 + 6/7 - 8/9 + ------- upto n terms

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.