Find Nth term of the series 3, 14, 39, 84…
Last Updated :
28 Jul, 2022
Given a number N. The task is to write a program to find the Nth term in the below series:
3, 14, 39, 84…
Examples:
Input: 3
Output: 39
For N = 3
Nth term = ( 3*3*3 ) + ( 3*3 ) + 3
= 39
Input: 4
Output: 84
Formula to calculate the Nth term of the series:
Nth term = ( N*N*N ) + ( N*N ) + N
Below is the required implementation:
C++
#include <iostream>
using namespace std;
int nthTerm( int N)
{
return (N * N * N) + (N * N) + N;
}
int main()
{
int N = 3;
cout << nthTerm(N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
public int nthTerm( int N)
{
return (N * N * N) + (N * N) + N;
}
public static void main(String[] args)
{
int N = 3 ;
GFG a = new GFG();
System.out.println(a.nthTerm(N));
}
}
|
Python 3
def nthTerm(n) :
return (N * N * N) + (N * N) + N
if __name__ = = "__main__" :
N = 3
print (nthTerm(N))
|
C#
using System;
class GFG
{
public int nthTerm( int N)
{
return (N * N * N) + (N * N) + N;
}
public static void Main()
{
int N = 3;
GFG a = new GFG();
Console.WriteLine(a.nthTerm(N));
}
}
|
PHP
<?php
function nthTerm( $N )
{
return ( $N * $N * $N ) +
( $N * $N ) + $N ;
}
$N = 3;
echo nthTerm( $N );
?>
|
Javascript
<script>
function nthTerm( N)
{
return (N * N * N) + (N * N) + N;
}
let N = 3;
document.write(nthTerm(N));
</script>
|
Time complexity: O(1) because performing constant operations
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...