Find Nth term of the series 0, 6, 0, 12, 0, 90…
Given a number N. The task is to write a program to find the Nth term in the below series:
0, 6, 0, 12, 0, 90…
Examples:
Input : N = 4
Output : 12
For N = 4
Nth term = abs( 4 * ((4-1) * (4-3) * (4-5)) );
= 12
Input : N = 6
Output : 90
We can generalize the Nth term of the above series as:
Nth term = abs( N * ((N-1) * (N-3) * (N-5)) )
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int nthTerm( int N)
{
return abs (N * ((N - 1) * (N - 3) * (N - 5)));
}
int main()
{
int N = 6;
cout << nthTerm(N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
class GFG {
public static int nthTerm( int N)
{
return Math.abs(N * ((N - 1 ) * (N - 3 ) * (N - 5 )));
}
public static void main(String[] args)
{
int N = 6 ;
System.out.println(nthTerm(N));
}
}
|
Python3
def nthTerm(N) :
return ( abs (N * ((N - 1 ) * (N - 3 )
* (N - 5 ))))
if __name__ = = "__main__" :
N = 6
print (nthTerm(N))
|
C#
using System;
class GFG {
public static int nthTerm( int N)
{
return Math.Abs(N * ((N - 1) * (N - 3) * (N - 5)));
}
public static void Main()
{
int N = 6;
Console.WriteLine(nthTerm(N));
}
}
|
PHP
<?php
function nthTerm( $N )
{
return abs ( $N * (( $N - 1) *
( $N - 3) * ( $N - 5)));
}
$N = 6;
echo nthTerm( $N );
?>
|
Javascript
<script>
function nthTerm( N)
{
return Math.abs(N * ((N - 1) * (N - 3) * (N - 5)));
}
let N = 6;
document.write( nthTerm(N) );
</script>
|
Time Complexity: O(1)
Space Complexity: O(1) since using constant variables
Last Updated :
28 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...