Open In App

Program for N-th term of Arithmetic Progression

Last Updated : 15 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given first term (a), common difference (d) and a integer N of the Arithmetic Progression series, the task is to find Nthterm of the series.
Examples :

Input : a = 2 d = 1 N = 5
Output : 6
The 5th term of the series is 6. The terms are 2, 3, 4, 5, 6, ....

Input : a = 5 d = 2 N = 10
Output : 23
The 10th term of the series is : 23 

Naive Solution

Using a loop to iterate over each term in the series until the N-th term is reached.

C++
#include <iostream>
using namespace std;

int main()
{
    int a = 2;
    int d = 1;
    int n = 5;
    
    int nthTerm = a;
    for (int i = 1; i < n; i++)
    {
        nthTerm += d;
    }
    cout << "The " << n << "th term of the series is: " 
         << nthTerm << endl;
    return 0;
}
C
// C code
#include <stdio.h>

int main() {
    int a = 2;
    int d = 1;
    int n = 5;
    
    int nthTerm = a;
    for (int i = 1; i < n; i++) {
        nthTerm += d;
    }
    printf("The %dth term of the series is: %d\n", n, nthTerm);
    return 0;
}
Java
// Java code
public class GfG {
    public static void main(String[] args) {
        int a = 2;
        int d = 1;
        int n = 5;
        
        int nthTerm = a;
        for (int i = 1; i < n; i++) {
            nthTerm += d;
        }
        System.out.println("The " + n + "th term of the series is: " + nthTerm);
    }
}
Python
# Python 3 code
a = 2

d = 1
n = 5

nthTerm = a
for i in range(1, n):
    nthTerm += d
print(f'The {n}th term of the series is: {nthTerm}')
C#
// C# code
using System;

class Program {
    static void Main() {
        int a = 2;
        int d = 1;
        int n = 5;
        
        int nthTerm = a;
        for (int i = 1; i < n; i++) {
            nthTerm += d;
        }
        Console.WriteLine($"The {n}th term of the series is: {nthTerm}");
    }
}
JavaScript
// JavaScript code
let a = 2;
let d = 1;
let n = 5;

let nthTerm = a;
for (let i = 1; i < n; i++) {
    nthTerm += d;
}
console.log(`The ${n}th term of the series is: ${nthTerm}`);
PHP
<?php
// PHP code
a = 2;
$d = 1;
$n = 5;

$nthTerm = $a;
for ($i = 1; $i < $n; $i++) {
    $nthTerm += $d;
}
echo "The {$n}th term of the series is: {$nthTerm}\n";
?>

Output
The 5th term of the series is: 6

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

Expected Approach

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

We know the Arithmetic Progression series is like = 2, 5, 8, 11, 14 …. … 
In this series 2 is the stating term of the series . 
Common difference = 5 – 2 = 3 (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) * d

C++
// CPP Program to find nth term of 
// Arithmetic progression
#include <bits/stdc++.h>
using namespace std;

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

// Driver code
int main() 
{
    // starting number
    int a = 2; 
    
    // Common difference
    int d = 1; 
    
    // N th term to be find
    int N = 5; 
    
    // Display the output
    cout << "The "<< N 
         <<"th term of the series is : "
         << Nth_of_AP(a,d,N);

    return 0;
}
Java
// Java program to find nth term
// of Arithmetic progression
import java.io.*;
import java.lang.*;

class GFG 
{
    public static int Nth_of_AP(int a, 
                                int d, 
                                int N)
    { 
        // using formula to find the Nth
        // term t(n) = a(1) + (n-1)*d
        return ( a + (N - 1) * d );
    }

    // Driver code
    public static void main(String[] args) 
    { 
        // starting number
        int a = 2; 
        
        // Common difference
        int d = 1; 
        
        // N th term to be find
        int N = 5; 

        // Display the output
        System.out.print("The "+ N + 
                         "th term of the series is : " +
                          Nth_of_AP(a, d, N));
    }
}
Python
# Python 3 Program to
# find nth term of 
# Arithmetic progression

def Nth_of_AP(a, d, N) :

    # using formula to find the 
    # Nth term t(n) = a(1) + (n-1)*d
    return (a + (N - 1) * d)
     
 
# Driver code
a = 2  # starting number
d = 1  # Common difference
N = 5  # N th term to be find
 
# Display the output
print( "The ", N ,"th term of the series is : ",
       Nth_of_AP(a, d, N))
C#
// C# program to find nth term
// of Arithmetic progression
using System;

class GFG 
{
    public static int Nth_of_AP(int a, 
                                int d, 
                                int N)
    { 
        
        // using formula to find the Nth
        // term t(n) = a(1) + (n-1)*d
        return ( a + (N - 1) * d );
    }

    // Driver code
    public static void Main() 
    { 
        // starting number
        int a = 2; 
        
        // Common difference
        int d = 1; 
        
        // N th term to be find
        int N = 5; 

        // Display the output
        Console.WriteLine("The "+ N + 
                          "th term of the series is : " +
                           Nth_of_AP(a, d, N));
    }
}

// This code is contributed by vt_m.
JavaScript
// JavaScript Program to find nth term of 
// Arithmetic progression

function Nth_of_AP(a, d, N) {
    // using formula to find the 
    // Nth term t(n) = a(1) + (n-1)*d
    return (a + (N - 1) * d);
}
 
// Driver code
let a = 2;  // starting number
let d = 1;  // Common difference
let N = 5;  // N th term to be find
 
// Display the output
console.log("The ", N, "th term of the series is : ", Nth_of_AP(a, d, N));
PHP
<?php
// PHP Program to find nth term of 
// Arithmetic progression

function Nth_of_AP($a, $d, $N)
{ 
    // using formula to find the 
    // Nth term t(n) = a(1) + (n-1)*d
    return ($a + ($N - 1) * $d);
    
}

// Driver code

// starting number
$a = 2; 

// Common difference
$d = 1; 

// N th term to be find
$N = 5; 
    
// Display the output
echo("The " . $N . "th term of the series is : " .
                           Nth_of_AP($a, $d, $N));

// This code is contributed by Ajit.
?>

Output
The 5th term of the series is : 6

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



Similar Reads