Nth term of AP from First Two Terms

Last Updated : 23 Jul, 2025

Given two integers a1 and a2, the first and second terms of an Arithmetic Series respectively, the problem is to find the nth term of the series. 
Examples :

Input : a1 = 2, a2 = 3, n = 4
Output : 5
Explanation : The series is 2, 3, 4, 5, 6, .... , thus the 4th term is 5.

Input : a1 = 1, a2 = 3, n = 10
Output : 19
Explanation: The series is: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21..... Thus,10th term is 19.

Try It Yourself
redirect icon

[Naive Approach] - Using for Loop

In an Arithmetic Series, the difference between all pair of consecutive terms is same, for example, 2, 5, 8, 11, 14,,,,, The common difference is 3.

  • Find the common difference of the series, common difference d = a2 - a1
  • Run a loop to iterate over each term in the series from a1, keep adding common difference d until the n-th term is reached.
C++
#include <bits/stdc++.h>
using namespace std;

int nthTermOfAP(int a1, int a2, int n)
{
    int nthTerm = a1, d = a2 - a1;

    for (int i = 1; i < n; i++){
        nthTerm += d;
    }
    return nthTerm;
}

int main()
{
    int a1 = 2, a2 = 3;
    int n = 4;

    cout << nthTermOfAP(a1, a2, n);
    return 0;
}
C
#include <stdio.h>

int nthTermOfAP(int a1, int a2, int n)
{
    int nthTerm = a1, d = a2 - a1;
    for (int i = 1; i < n; i++)
    {
        nthTerm += d;
    }
    return nthTerm;
}

int main()
{
    int a1 = 2, a2 = 3;
    int n = 4;
    printf("%d", nthTermOfAP(a1, a2, n));
    return 0;
}
Java
public class Main {
    public static int nthTermOfAP(int a1, int a2, int n)
    {
        int nthTerm = a1, d = a2 - a1;
        for (int i = 1; i < n; i++) {
            nthTerm += d;
        }
        return nthTerm;
    }

    public static void main(String[] args)
    {
        int a1 = 2, a2 = 3;
        int n = 4;
        System.out.println(nthTermOfAP(a1, a2, n));
    }
}
Python
def nthTermOfAP(a1, a2, n):
    nthTerm = a1
    d = a2 - a1
    for i in range(1, n):
        nthTerm += d
    return nthTerm


a1 = 2
a2 = 3
n = 4
print(nthTermOfAP(a1, a2, n))
C#
using System;

class Program {
    static int nthTermOfAP(int a1, int a2, int n)
    {
        int nthTerm = a1;
        int d = a2 - a1;
        for (int i = 1; i < n; i++) {
            nthTerm += d;
        }
        return nthTerm;
    }

    static void Main()
    {
        int a1 = 2, a2 = 3;
        int n = 4;
        Console.WriteLine(nthTermOfAP(a1, a2, n));
    }
}
JavaScript
function nthTermOfAP(a1, a2, n)
{
    let nthTerm = a1;
    let d = a2 - a1;
    for (let i = 1; i < n; i++) {
        nthTerm += d;
    }
    return nthTerm;
}

let a1 = 2, a2 = 3;
let n = 4;
console.log(nthTermOfAP(a1, a2, n));

Output
5

Time Complexity - O(n)
Auxiliary Space - O(1)

[Expected Approach] - Using the Formula for nth Term

To find the nth term in the Arithmetic Progression series we use the simple formula . 

We know the Arithmetic Progression series is like = 2, 3, 4, 5, 6. …. … 
In this series 2 is the first term and 3 is the second term of the series . 
Common difference = a2 - a1 = 3 – 2 = 1 (Difference common in the series). 
so we can write the series as :
t1 = a1 
t2 = a1 + (2-1) * d 
t3 = a1 + (3-1) * d 



tN = a1 + (n-1) * d

tN = a1 + (n-1) * (a2-a1)

C++
#include <bits/stdc++.h>
using namespace std;

int nthTermOfAP(int a1, int a2, int n)
{
    // using formula to find the
    // Nth term t(n) = a(1) + (n-1)*d
    return (a1 + (n - 1) * (a2 - a1));
}

int main()
{
    int a1 = 2, a2 = 3;
    int n = 4;
    cout << nthTermOfAP(a1, a2, n);
    return 0;
}
C
#include <stdio.h>

int nthTermOfAP(int a1, int a2, int n)
{
    // using formula to find the
    // Nth term t(n) = a(1) + (n-1)*d
    return (a1 + (n - 1) * (a2 - a1));
}

int main()
{
    int a1 = 2, a2 = 3;
    int n = 4;
    printf("%d", nthTermOfAP(a1, a2, n));
    return 0;
}
Java
public class Main {
    public static int nthTermOfAP(int a1, int a2, int n)
    {
        // using formula to find the
        // Nth term t(n) = a(1) + (n-1)*d
        return (a1 + (n - 1) * (a2 - a1));
    }

    public static void main(String[] args)
    {
        int a1 = 2, a2 = 3;
        int n = 4;
        System.out.println(nthTermOfAP(a1, a2, n));
    }
}
Python
def nthTermOfAP(a1, a2, n):
    # using formula to find the
    # Nth term t(n) = a(1) + (n-1)*d
    return a1 + (n - 1) * (a2 - a1)


a1 = 2
a2 = 3
n = 4
print(nthTermOfAP(a1, a2, n))
C#
using System;

class Program {
    static int nthTermOfAP(int a1, int a2, int n)
    {
        // using formula to find the
        // Nth term t(n) = a(1) + (n-1)*d
        return (a1 + (n - 1) * (a2 - a1));
    }

    static void Main()
    {
        int a1 = 2, a2 = 3;
        int n = 4;
        Console.WriteLine(nthTermOfAP(a1, a2, n));
    }
}
JavaScript
function nthTermOfAP(a1, a2, n)
{
    // using formula to find the
    // Nth term t(n) = a(1) + (n-1)*d
    return a1 + (n - 1) * (a2 - a1);
}

let a1 = 2, a2 = 3;
let n = 4;
console.log(nthTermOfAP(a1, a2, n));

Output
5

Time Complexity - O(1)
Auxiliary Space - O(1)

Comment