Open In App

JavaScript Program to find nth term of Arithmetic Progression

Last Updated : 14 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Arithmetic progression is a sequence of numbers in which the difference between any two consecutive terms is always equal. This difference between any two consecutive terms is known as a common difference and it can be positive, negative or zero.

The nth term of A.P. in mathematics is calculated by using the nth term formula which is described as follows:

Syntax:

an  = a + (n-1) × d

Parameters:

  • a: It is the first term of the A.P. series.
  • n: It is the position or index of the term in the A.P. series.
  • d: It is a common difference between the two terms of an A.P.
  • an: It is the nth term in the A.P. series

Example: To demonstrate finding the the nth terms in the A.P. series using the function which uses the nth term formula to calculate the nth terms of an A.P, series to print the result.

Javascript
// Define the function to find the nth term of A.P.
function findNthTerm(firstTerm, commonDifference, n) {
    return firstTerm + (n - 1) * commonDifference;
}

// First term of the A.P.
const firstTerm = 2;

// Common difference of the A.P.
const commonDifference = 3;

// The nth term we want to calculate
const n = 15;

// Call the function 
const nthTerm = findNthTerm(firstTerm, commonDifference, n);
console.log(
    `The ${n}-th term of the arithmetic progression is: ${nthTerm}`);

Output
The 15-th term of the arithmetic progression is: 44

Time Complexity : O(1) , constant time

Space Complexity : O(1), constant space


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads