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


  • yash

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

    }
    else{
    ans = pow(2,(N-1)/2);
    }
    cout<<ans<<endl;

    }


  • Tejdeep

    #include
    using namespace std;
    int main()
    {
    int x = 2;
    int y = 3;
    int n;
    int b;
    int u;
    cout << "please enter the value of n"<> n;
    b = pow(x,n);
    u = pow(y,n);
    cout << "value is even " << b << endl;
    cout << "value is odd" << u << endl;
    }

    Can we get output like this??
    Mech guy, just tried…………….


  • Man of

    int x2x3(int n){
    if(n%2==0){
    n=n/2;
    return pow(3,n-1);
    }
    else{
    n=n/2 + 1;
    return pow(2,n-1);
    }
    }


  • Satya Sai Srija

    r_odd=2
    r_even=3
    r_a=1
    i=int(input())
    if i%2==0:
    i=int(i/2)-1
    print(r_a*(r_even**i))
    else:
    i=int((i+1)/2)-1
    print(r_a*(r_odd**i))


  • Prajjwal

    int main()
    {
    int n;
    scanf(“%d”,&n);
    if(n%2==0)
    three(n/2);
    else
    two(n-1);
    }

    void two(int n){
    int y;
    y=pow(2,n/2);
    printf(“%d”,y);

    }

    void three(int n){
    int y;
    y=pow(3,n-1);
    printf(“%d”,y);

    }


  • Cynosure

    If for the same question ,input be given –
    Input Format

    Following the above series in input

    Constraints

    1<=N<=10000

    Output Format

    Print the Nth term in the series

    Sample Input 0

    1
    1
    2
    3
    Sample Output 0

    4

    Then please write code for this


  • siddhesh

    //something simple for printing the whole series and also for printing the Nth element.
    #include
    #include
    int main()
    {
    int n,a=0,b=0,i;
    scanf(“%d”,&n);
    for(i=1;i<=n;i++)
    {
    if(i==1)
    {
    a=1;
    //printf("%d ",a);
    }
    else if(i==2)
    {
    b=1;
    //printf("%d ",b);
    }
    else if(i%2==0)
    {
    a=a*3;
    //printf("%d ",a);
    }
    else
    {
    b=b*2;
    //printf("%d ",b);
    }

    }

    if(n%2==0)
    {
    printf("%d",a);
    }
    else
    {
    printf("%d",b);
    }
    return 0;
    }


    • Yaswanth a

      Python:
      val=int(input())
      x=1
      y=1
      count=1
      ct=1
      for i in range (2,val+1):
      if (i<=1):
      x=1
      y=1
      if (i%2 ==0):
      x=2**count
      count+=1
      else:
      y=3**ct
      ct+=1
      if (val%2!=0):
      print("{} term in program is {}".format(val,y))
      else:
      print("{} term in program is {}".format(val,x))

      #This is contributed by YASWANTH A


    • Yuvraj

      import math
      lst = []
      n = int(input())
      for i in range(math.ceil(n/2)):
      lst.append(pow(2,i))
      lst.append(pow(3,i))
      if n % 2 == 0:
      print(lst[-1])
      else:
      print(lst[-2])


  • Jbl

    Hey man…ur ans in c language was wrong…because if n=8 then it moves to three(n/2)..thus n=4 and in the sub function for i=4 ;x will be 81…if i==n i.e.4=4..then 81 will be printed…but the answer to be printed is 27…before uploading into internet..check it once man..u waste fellows….


  • J.Sravani

    #include

    int main()
    {
    int i,a=0,b=0,k=0,j=0,n;
    printf(“enter the value”);
    scanf(“%d”,&n);
    printf(“%d%d”,a,b);
    for (i=0;i<n;i++)
    {
    j=j+7;
    printf(" %d",j);
    k=k+6;
    printf(" %d",k);
    }
    return 0;
    }


    • Gowsalya

      #include
      #include
      int arr[100];

      int main()
      {
      arr[0]=1;
      arr[1]=1;
      int j=1,k=1;
      int pos,i;
      scanf(“%d”,&pos);
      if(pos==0||pos==1)
      {
      printf(“1”);
      }
      for(i=2;i<=pos;i++)
      {

      if(i%2!=0)
      {
      arr[i]=pow(3,j++);
      }
      else
      {
      arr[i]=pow(2,k++);
      }
      }
      /*for(i=0;i<=pos;i++)
      {
      printf("%d",arr[i]);
      }*/
      printf("%d",arr[pos]);
      }


      • Krishanu

        c++ program :
        ==========================
        #include
        using namespace std;
        int main()
        {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        int n,flag=0,s=1;
        cin>>n;
        if(n%2 != 0)
        {
        flag =1;
        n=n+1;
        }
        if(flag ==1)
        {
        n/=2;
        while(n–>1)
        {
        s=s*2;
        }
        }
        else
        {
        n/=2;
        while(n–>1)
        s*=3;
        }
        cout<<s;
        }