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 :
The 5th term of the series is : 6
Input : a = 5 d = 2 N = 10
Output :
The 10th term of the series is : 23
Approach:
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
To find the Nth term in the Arithmetic Progression series we use the simple formula .
TN = a1 + (N-1) * d
C++
#include <bits/stdc++.h>
using namespace std;
int Nth_of_AP(int a, int d, int N)
{
return (a + (N - 1) * d);
}
int main()
{
int a = 2;
int d = 1;
int N = 5;
cout << "The "<< N
<<"th term of the series is : "
<< Nth_of_AP(a,d,N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
class GFG
{
public static int Nth_of_AP(int a,
int d,
int N)
{
return ( a + (N - 1) * d );
}
public static void main(String[] args)
{
int a = 2;
int d = 1;
int N = 5;
System.out.print("The "+ N +
"th term of the series is : " +
Nth_of_AP(a, d, N));
}
}
|
Python3
def Nth_of_AP(a, d, N) :
return (a + (N - 1) * d)
a = 2
d = 1
N = 5
print( "The ", N ,"th term of the series is : ",
Nth_of_AP(a, d, N))
|
C#
using System;
class GFG
{
public static int Nth_of_AP(int a,
int d,
int N)
{
return ( a + (N - 1) * d );
}
public static void Main()
{
int a = 2;
int d = 1;
int N = 5;
Console.WriteLine("The "+ N +
"th term of the series is : " +
Nth_of_AP(a, d, N));
}
}
|
Javascript
<script>
function Nth_of_AP(a, d, N)
{
return (a + (N - 1) * d);
}
let a = 2;
let d = 1;
let N = 5;
document.write("The "+ N + "th term of the series is : "
+ Nth_of_AP(a,d,N));
</script>
|
PHP
<?php
function Nth_of_AP($a, $d, $N)
{
return ($a + ($N - 1) * $d);
}
$a = 2;
$d = 1;
$N = 5;
echo("The " . $N . "th term of the series is : " .
Nth_of_AP($a, $d, $N));
?>
|
Output :
The 5th term of the series is : 6
Time Complexity: O(1), the code will run in a constant time.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Approach 2: 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;
}
|
Java
import java.util.*;
public class Main {
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);
}
}
|
Python3
a = 2
d = 1
n = 5
nthTerm = a
for i in range(1, n):
nthTerm += d
print("The", n, "th term of the series is:", nthTerm)
|
C#
using System;
public class MainClass
{
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;
}
Console.WriteLine("The " + n + "th term of the series is: " + nthTerm);
}
}
|
Javascript
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
$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;
?>
# This code is contributed by ameyabavkar02
|
OutputThe 5th term of the series is: 6
Time complexity:- O(N), Where N is the term to be found
Space complexity :- O(1)
Last Updated :
01 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...