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.

Link to this Question

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

42 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,….”


  • Gyanendra

    import java.util.*;
    public class Main
    {
    public static void main(String[] args) {
    Scanner scn=new Scanner(System.in);
    int n=scn.nextInt();

    int val1=1;
    int val2=1;

    if(n%2==0){
    for(int i=4;i<=n;i+=2){
    val1*=3;
    }
    System.out.println(val1);
    }
    else{
    for(int i=3;i<=n;i+=2){
    val2*=2;
    }

    System.out.println(val2);
    }
    }
    }


  • anushkav220

    public static void main(String[] args) {
    Scanner s=new Scanner(System.in);
    int n=s.nextInt();
    int output=0;

    if(n%2==0) {

    output=(int)Math.pow(3,(n-1)/2);

    System.out.println(output);
    }
    else {

    output=(int)Math.pow(2,(n-1)/2);

    System.out.println(output);
    }

    }


  • Manoj

    #python code by Manoj thatipigari
    n=int(input())
    a=1
    if n%2==0:
    for i in range(0,(n//2)-1):
    a=a*3
    print(a)
    elif n%2==1:
    for i in range(0,((n+1)//2)-1):
    a=a*2
    print(a)


  • savita

    n = int(input(“enter the number”))
    a=0
    b=0
    c=0
    for i in range(0,n):
    if(i % 2 != 0):
    if(i==1):
    a = 1;
    else:
    a = a*2;
    else:
    if(i == 2):
    b = 2;
    else:
    b = 3*b;

    print(a)
    print(b)
    i have used python code for above code ,here output doesn’t match please correct this code


    • Manoj

      n=int(input())
      a=1
      if n%2==0:
      for i in range(0,(n//2)-1):
      a=a*3
      print(a)
      elif n%2==1:
      for i in range(0,((n+1)//2)-1):
      a=a*2
      print(a)


  • muskan

    — Found this from tv nagaraju Technical channel Youtube
    #include
    #include
    int power(int base, int exp)
    {
    int p=1,i;
    for(i=1;i<=exp;i++)
    {
    p=p*base;
    }
    return p;
    }
    void main(int argc, char argv[])
    {
    int n,x,res;
    n=atoi(argv[1]);
    if(n%2==0)
    {
    x=n/2;
    res=power(3,x-1);
    }
    else{
    x=n/2+1;
    res=power(2,x-1);
    }
    printf("%d",res);
    }


  • Bhuvanesh

    val=int(input())
    x,y=1,1
    for i in range(3,val+1):
    if i%2 != 0:
    x+=x
    else:
    y*=3

    if val%2==0:
    print(y)
    else:
    print(x)


  • Sheeri

    import java.util.Scanner;

    public class mainClass{
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println(“enter the number “);
    int N = sc.nextInt();

    System.out.println(N%2==0 ? (int)Math.pow(3, ((N/2)-1)) : (int)Math.pow(2, (N) ));

    }

    }


    • Sheeri

      CORRECTION:

      import java.util.Scanner;

      public class mainClass {
      public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.println(“enter the number “);
      int N = sc.nextInt();

      System.out.println(N%2==0 ? (int)Math.pow(3, ((N/2)-1)) : (int)Math.pow(2, (N/2) ));

      }

      }


  • GRAHEETH

    #include
    using namespace std;
    int main(){
    int n;
    cin>>n;
    int ans;
    if(n%2==0){
    ans=pow(3,(n/2)-1);
    }
    else{
    ans=pow(2,n/2);
    }
    cout<<ans;

    }


  • mekala

    Sekhar python code:
    l1=[1]
    l2=[1]
    for i in range(15):
    l1.append(l1[i]*2)
    l2.append(l2[i]*3)

    n=int(input())
    if n%2==0:
    print(l2[int(n/2)-1])
    else:
    import math
    k=math.floor(n/2)
    print(l1[k])


  • Jatin

    public class Main {
    public static void main(String args[]) {
    int n = 5 ;
    int ans = 0 ;
    if(n %2==0){
    int ans1 = n/2;
    int any = ans1-1 ;
    ans = (int)Math.pow(3,any) ;

    }
    else{
    int power = n/2 ;
    ans = (int)Math.pow(2,power) ;

    }
    System.out.print(ans) ;
    }
    }