Find Nth term of the series 1, 1, 2, 6, 24…

Given a number N. The task is to write a program to find the Nth term in the below series:
1, 1, 2, 6, 24…
Examples:
Input: 3
Output: 2
For N = 3
Nth term = (N-1)!
= 2
Input: 5
Output: 24
Nth term of the series is given by below formula:
Nth term = ( N-1)!
Below is the required implementation:
C++
// CPP program to find N-th term of the series:// 1, 1, 2, 6, 24...#include <iostream>using namespace std;// calculate Nth term of seriesint nthTerm(int N){ if (N <= 1) return 1; int i, fact = 1; for (i = 1; i < N; i++) fact = fact * i; return fact;}// Driver Functionint main(){ int N = 3; cout << nthTerm(N); return 0;} |
Java
// Java program to find the Nth termimport java.io.*;// calculate Nth term of this series// 1, 1, 2, 6, 24...class Nth { public int nthTerm(int N) { // By using above formula if (N <= 1) return 1; int i, fact = 1; for (i = 1; i < N; i++) fact = fact * i; return fact; }}// Main class for main methodclass GFG { public static void main(String[] args) { int N = 3; // create object of Class Nth Nth a = new Nth(); // call and print Nth term System.out.println(a.nthTerm(N)); }} |
Python 3
# Python 3 program to find # N-th term of the series: # 1, 1, 2, 6, 24... # Function to calculate # Nth term of series def nthTerm(n) : if n <= 1 : return 1 fact = 1 for i in range(1, N) : fact = fact * i return fact# Driver codeif __name__ == "__main__" : N = 3 # function calling print(nthTerm(N))# This code is contributed # by ANKITRAI1 |
C#
// C# program to find the // Nth term of the series// 1, 1, 2, 6, 24...using System;// calculate Nth term class GFG {public int nthTerm(int N){ // By using above formula if (N <= 1) return 1; int i, fact = 1; for (i = 1; i < N; i++) fact = fact * i; return fact;}// Driver Codepublic static void Main(){ int N = 3; // create object of Class GFG GFG a = new GFG(); // call and print Nth term Console.Write(a.nthTerm(N));}}// This code is contributed // by ChitraNayal |
PHP
<?php// PHP program to find N-th // term of the series:// 1, 1, 2, 6, 24...// calculate Nth term of seriesfunction nthTerm($N){ if ($N <= 1) return 1; $fact = 1; for ($i = 1; $i < $N; $i++) $fact = $fact * $i; return $fact;}// Driver Code$N = 3;echo nthTerm($N);// This code is contributed // by ChitraNayal?> |
Javascript
<script>// JavaScript program to find N-th term of the series:// 1, 1, 2, 6, 24...// calculate Nth term of seriesfunction nthTerm( N){ if (N <= 1) return 1; let i, fact = 1; for (i = 1; i < N; i++) fact = fact * i; return fact;}// Driver Function let N = 3; document.write(nthTerm(N)); // This code contributed by Rajput-Ji</script> |
Output:
2
Time Complexity: O(N), where N represents the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



