# TCS Write a Program Find the nth term of the series. 1,1,2,3,4,9,8,27,16,81,32,243,….

### Problem

Question. Find the nth term of the series.

1, 1, 2, 3, 4, 9, 8, 27, 16, 81, 32, 243,64, 729, 128, 2187 ….

This series is a mixture of 2 series – all the odd terms in this series form a geometric series and all the even terms form yet another geometric series. Write a program to find the Nth term in the series.

• The value N in a positive integer that should be read from STDIN.
• The Nth term that is calculated by the program should be written to STDOUT.
• Other than value of n th term,no other character / string or message should be written to STDOUT.
• For example , if N=16, the 16th term in the series is 2187, so only value 2187 should be printed to STDOUT.

You can assume that N will not exceed 30.

Test Case 1

• Input- 16
• Expected Output – 2187

Test Case 2

• Input- 13
• Expected Output – 64

(TCS Ninja – Dec 2018 Slot 2)

### Explanation

1, 1, 2, 3, 4, 9, 8, 27, 16, 81, 32, 243,64, 729, 128, 2187 can represented as :

• 2(0), 3(0),2(1), 3(1),2(2), 3(2),2(3), 3(3),2(4), 3(4),2(5), 3(5),2(6), 3(6) ….

There are two consecutive sub GP’s at even and odd positions

• (GP-1) At Odd Positions (Powers of 2) – 1, 2, 4, 8, 16, 32, 64, 128
• (GP-2) At Even Positions (Powers of 3) – 1, 3, 9, 27, 81, 243, 729, 2187

Clearly, for calculating Nth position value

• If N is Even, Find (N/2) position in sub GP – 2
• If N is Odd, Find (N/2 + 1) position in sub GP – 1
```#include<stdio.h>
#include<math.h>

int three(int n)
{
int x;
//n-1 because powers start from 0 not 1
x = pow(3,n-1);
printf("%d",x);
}

int two(int n)
{
int x;
//n-1 because powers start from 0 not 1
x = pow(2,n-1);
printf("%d",x);
}

int main()
{
int n;
scanf("%d",&n);

//Checking of the nth term will be at even position or odd position
//Odd positions are powers of 2
//Even positions are powers of 3
if(n%2==0)
{
//nth position(if even) will be at n/2 position for sub GP-2
three(n/2);
}
else
{
//nth position(if odd) will be at (n/2 + 1) position for sub GP-1
two(n/2 + 1);
}
return 0;
}```
```#include<iostream>
#include<math.h>using namespace std;

int three(int n)
{
int x;
//n-1 because powers start from 0 not 1
x = pow(3,n-1);
cout<< x;
}

int two(int n)
{
int x;
//n-1 because powers start from 0 not 1
x = pow(2,n-1);
cout<< x;
}

int main()
{
int n;    cin >> n;

//Checking of the nth term will be at even position or odd position
//Odd positions are powers of 2
//Even positions are powers of 3
if(n%2==0)
{
//nth position(if even) will be at n/2 position for sub GP-2
three(n/2);
}
else
{
//nth position(if odd) will be at (n/2 + 1) position for sub GP-1
two(n/2 + 1);
}
return 0;
}```
```import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sin = new Scanner(System.in);
int n = sin.nextInt();
System.out.println(n%2==0?(int)Math.pow(3,(n-1)/2):(int)Math.pow(2,(n-1)/2));
}
}
// we have used ternary operator above condition ? value_if_true : value_if_false```
`num = int(input())if(num%2==0):num = num // 2print(3**(num-1))else:num = num // 2 + 1print(2**(num-1))`

### TCS Coding Questions

All Coding Questions for TCS

Very Important

### 17 comments on “TCS Write a Program Find the nth term of the series. 1,1,2,3,4,9,8,27,16,81,32,243,….”

• Danish

#include
#include
using namespace std;
int main()
{
int n;
cout<>n;
int j=0,k=0;
for(int i=0;i<n;i++)
{
if(i%2 ==0)
{
cout<<pow(2,j)<<" ";
j++;
}
if(i%2!=0)
{
cout<<pow(3,k)<<" ";
k++;
}
}
} 0