Program to find Nth term in the given Series - GeeksforGeeks

01 Nov, 2021
Given a number N. The task is to write a program to find the N-th term in the below series: 
1, 1, 2, 3, 4, 9, 8, 27, 16, 81, 32, 243, 64, 729, 128, 2187...
Examples:  
Input : 4
Output : 3

Input :  11
Output : 32
 
On observing carefully, you will find that the series is a mixture of 2 series:  
All the odd terms in this series form a geometric series.All the even terms form yet another geometric series.
All the odd terms in this series form a geometric series.
All the even terms form yet another geometric series.
The approach to solving the problem is quite simple. The odd positioned terms in the given series form a GP series with first term = 1 and common ration = 2. Similarly, the even positioned terms in the given series form a GP series with first term = 1 and common ration = 3.Therefore first check whether the input number N is even or odd. If it is even, set N=N/2(since there are Two GP series running parallelly) and find the Nth term by using formula an = a1·rn-1 with r=3. Similarly, if N is odd, set N=(n/2)+1 and do the same as previous with r=2.Below is the implementation of above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find Nth term// in the given Series#include <iostream>#include <math.h> using namespace std; // Function to find the nth term// in the given seriesvoid findNthTerm(int n){    // If input number is even    if (n % 2 == 0) {        n = n / 2;        cout << pow(3, n - 1) << endl;    }    // If input number is odd    else {        n = (n / 2) + 1;        cout << pow(2, n - 1) << endl;    }} // Driver Codeint main(){    int N = 4;    findNthTerm(N);     N = 11;    findNthTerm(N);     return 0;}
// Java program to find Nth term// in the given Seriesimport java.io.*;import java.util.*;import java.lang.*; class GFG{// Function to find the nth term// in the given seriesstatic void findNthTerm(int n){    // If input number is even    if (n % 2 == 0)    {        n = n / 2;        System.out.print(Math.pow(3, n - 1) + "\n");    }    // If input number is odd    else    {        n = (n / 2) + 1;        System.out.print(Math.pow(2, n - 1) + "\n");    }} // Driver Codepublic static void main(String[] args){    int N = 4;    findNthTerm(N);     N = 11;    findNthTerm(N); }} // This code is contributed// by Akanksha Rai(Abby_akku)
# Python3 program to find Nth term# in the given Series # Function to find the nth term# in the given seriesdef findNthTerm(n):    # If input number is even    if n % 2 == 0:        n //= 2        print(3 ** (n - 1))    # If input number is odd    else:        n = (n // 2) + 1        print(2 ** (n - 1)) # Driver Codeif __name__=='__main__':    N = 4    findNthTerm(N)     N = 11    findNthTerm(N) # This code is contributed# by vaibhav29498
// C# program to find Nth term// in the given Seriesusing System; class GFG{// Function to find the nth// term in the given seriesstatic void findNthTerm(int n){    // If input number is even    if (n % 2 == 0)    {        n = n / 2;        Console.WriteLine(Math.Pow(3, n - 1));    }         // If input number is odd    else    {        n = (n / 2) + 1;        Console.WriteLine(Math.Pow(2, n - 1));    }} // Driver Codepublic static void Main(){    int N = 4;    findNthTerm(N);     N = 11;    findNthTerm(N);}} // This code is contributed// by chandan_jnu.
<?php// php program to find Nth term// in the given Series // Function to find the nth term// in the given seriesfunction findNthTerm($n){    // If input number is even    if ($n % 2 == 0)    {        $n = $n / 2;        echo pow(3, $n - 1) . "\n";    }    // If input number is odd    else    {        $n = ($n / 2) + 1;        echo pow(2, intval($n - 1)) . "\n";    }} // Driver Code$N = 4;findNthTerm($N); $N = 11;findNthTerm($N); // This code is contributed// by Akanksha Rai(Abby_akku)?>
<script> // JavaScript program to find Nth term // in the given Series     // Function to find the nth term     // in the given series    function findNthTerm(n)    {        // If input number is even        if (n % 2 == 0) {            n = Math.floor(n / 2);            document.write(Math.pow(3, n - 1) + "<br>");        }        // If input number is odd        else {            n = Math.floor(n / 2) + 1;            document.write(Math.pow(2, n - 1) + "<br>");        }    }       // Driver Code      let N = 4;    findNthTerm(N);       N = 11;    findNthTerm(N); // This code is contributed by Mayank Tyagi </script>
3
32
 
Akanksha_Rai
Chandan_Kumar
vaibhav29498
mayanktyagi1709
khushboogoyal499
school-programming
series
TCS
C++ Programs
TCS
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
cin in C++
CSV file management using C++
Shallow Copy and Deep Copy in C++
Passing a function as a parameter in C++
Const keyword in C++
Program to implement Singly Linked List in C++ using class
C++ Program to check if a given String is Palindrome or not
Generics in C++
cout in C++
Passing and Returning Objects in C++