Finite Arithmetic Progression

An arithmetic progression (AP) is a sequence of numbers in which the difference between consecutive terms is constant. This difference is called the “common difference,” denoted by (d). A finite arithmetic progression is simply an AP that has a fixed number of terms, (n). The first term is denoted as (a).

The (n)th term (T_n) of an AP can be calculated using the formula:
[ T_n = a + (n - 1) \times d ]

The sum (S) of the first (n) terms in an arithmetic sequence can be calculated using the formula:
[ S = \frac{n}{2} \times (a + T_n) ] or equivalently, [ S = \frac{n}{2} \times (2a + (n - 1) \times d) ]

Example Code

Java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public class ArithmeticProgression {
  public static void main(String[] args) {
    int a = 1;  // First term
    int d = 2;  // Common difference
    int n = 5;  // Number of terms

    // Calculate nth term and sum
    int Tn = a + (n - 1) * d;
    int sum = n * (a + Tn) / 2;

    System.out.println("The nth term is: " + Tn);
    System.out.println("The sum of the first n terms is: " + sum);
  }
}

C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main() {
  int a = 1;  // First term
  int d = 2;  // Common difference
  int n = 5;  // Number of terms
  
  // Calculate nth term and sum
  int Tn = a + (n - 1) * d;
  int sum = n * (a + Tn) / 2;
  
  cout << "The nth term is: " << Tn << endl;
  cout << "The sum of the first n terms is: " << sum << endl;

  return 0;
}

Python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# First term
a = 1
# Common difference
d = 2
# Number of terms
n = 5

# Calculate nth term and sum
Tn = a + (n - 1) * d
sum = n * (a + Tn) // 2

print(f"The nth term is: {Tn}")
print(f"The sum of the first n terms is: {sum}")

In these example codes, the formula for the (n)th term and the sum of the first (n) terms in a finite arithmetic progression are implemented in Java, C++, and Python.