Program to find Nth term of series 1, 3, 12, 60, 360…
Given a number N. The task is to write a program to find the Nth term in the below series:
1, 3, 12, 60, 360…
Examples:
Input: 2
Output: 3
Input: 4
Output: 60
Approach: The idea is to first find the factorial of the number (N+1), that is (N+1)!
Now, the N-th term in the above series will be:
N-th term = (N+1)!/2
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int factorial( int N)
{
int fact = 1;
for ( int i = 1; i <= N; i++)
fact = fact * i;
return fact;
}
int nthTerm( int N)
{
return (factorial(N + 1) / 2);
}
int main()
{
int N = 6;
cout << nthTerm(N);
return 0;
}
|
C
#include <stdio.h>
int factorial( int N)
{
int fact = 1;
for ( int i = 1; i <= N; i++)
fact = fact * i;
return fact;
}
int nthTerm( int N)
{
return (factorial(N + 1) / 2);
}
int main()
{
int N = 6;
printf ( "%d" ,nthTerm(N));
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
static int factorial( int N)
{
int fact = 1 ;
for ( int i = 1 ; i <= N; i++)
fact = fact * i;
return fact;
}
static int nthTerm( int N)
{
return (factorial(N + 1 ) / 2 );
}
public static void main(String args[])
{
int N = 6 ;
System.out.println(nthTerm(N));
}
}
|
Python3
def factorial(N) :
fact = 1
for i in range ( 1 , N + 1 ) :
fact = fact * i
return fact
def nthTerm(N) :
return (factorial(N + 1 ) / / 2 )
if __name__ = = "__main__" :
N = 6
print (nthTerm(N))
|
C#
using System;
class GFG
{
static int factorial( int N)
{
int fact = 1;
for ( int i = 1; i <= N; i++)
fact = fact * i;
return fact;
}
static int nthTerm( int N)
{
return (factorial(N + 1) / 2);
}
static void Main()
{
int N = 6 ;
Console.WriteLine(nthTerm(N));
}
}
|
PHP
<?php
function factorial( $N )
{
$fact = 1;
for ( $i = 1; $i <= $N ; $i ++)
$fact = $fact * $i ;
return $fact ;
}
function nthTerm( $N )
{
return (factorial( $N + 1) / 2);
}
$N = 6;
echo nthTerm( $N );
?>
|
Javascript
<script>
function factorial(N)
{
let fact = 1;
for (let i = 1; i <= N; i++)
fact = fact * i;
return fact;
}
function nthTerm(N)
{
return (Math.floor(factorial(N + 1) / 2));
}
let N = 6;
document.write(nthTerm(N));
</script>
|
Time Complexity: O(n), where n represents the given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Last Updated :
13 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...