Program to find the Nth term of series 5, 12, 21, 32, 45……
Given a number N, The task is to write a program to find the Nth term of the below series:
5, 12, 21, 32, 45……
Examples:
Input: N = 2
Output: 12
Input: N = 5
Output: 45
Approach:
The generalized Nth term of this series:
Nth Term : n*n + 4*n
Below is the required implementation:
C++
#include <iostream>
#include <math.h>
using namespace std;
int nthTerm( int n)
{
return pow (n, 2) + 4 * n;
}
int main()
{
int N = 4;
cout << nthTerm(N) << endl;
return 0;
}
|
C
#include <stdio.h>
#include <math.h>
int nthTerm( int n)
{
return pow (n, 2) + 4 * n;
}
int main()
{
int N = 4;
printf ( "%d\n" ,nthTerm(N));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int nthTerm( int n)
{
return ( int )Math.pow(n, 2 ) + 4 * n;
}
public static void main (String[] args) {
int N = 4 ;
System.out.println( nthTerm(N));
}
}
|
Python3
def nthTerm(n):
return n * * 2 + 4 * n;
N = 4
print (nthTerm(N))
|
C#
using System;
class GFG
{
static int nthTerm( int n)
{
return ( int )Math.Pow(n, 2) + 4 * n;
}
public static void Main ()
{
int N = 4;
Console.WriteLine(nthTerm(N));
}
}
|
PHP
<?php
function nthTerm( $n )
{
return pow( $n , 2) + 4 * $n ;
}
$N = 4;
echo nthTerm( $N );
?>
|
Javascript
<script>
function nthTerm( n)
{
return Math.pow(n, 2) + 4 * n;
}
let N = 4;
document.write( nthTerm(N));
</script>
|
Time complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.
Last Updated :
27 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...