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

Find the nth term of the series.

1,1,2,3,4,9,8,27,16,81,32,243,….

Please submit the code in other languages as well.

#include<stdio.h>
#include<math.h>
int three(n)
{
int x,i;
for(i=0;i<100;i++)
{
x=pow(3,i);
if(i==n)
printf(“%d”,x);
}
}
int two(n)
{
int x,i;
for(i=0;i<100;i++)
{
x=pow(2,i);
if(i==n)
printf(“%d”,x);
}
}
int main()
{
int n;
scanf(“%d”,&n);
if(n%2==0)
three(n/2);
else
two(n/2+1);
}

In Java

import java.util.ArrayList;
import java.util.Scanner;

public class NthTermInASeries {

public static void main(String[] args) {
// start!!!
// 1,1,2,3,4,9,8,27,16,81,32,243,….
int i, j, k = 1, m = 1, o = 0;
ArrayList<Integer> odd = new ArrayList<Integer>();
ArrayList<Integer> even = new ArrayList<Integer>();
ArrayList<Integer> result = new ArrayList<Integer>();
System.out.println(“enter the number”);
Scanner in = new Scanner(System.in);
int n = in.nextInt();
odd.add(1);
for (i = 1; i < n; i++) {
k = k * 2;
odd.add(k);

}
even.add(1);
for (j = 1; j < n; j++) {
m = (m * 3);
even.add(m);
}
System.out.println(odd);
System.out.println(even);

int f = 0;
int g = 0;
for (o = 0; o < 2 * n; o++) {
if (o % 2 == 0) {
result.add(odd.get(f));
f++;
} else {
result.add(even.get(g));
g++;
}
}
System.out.println(result);
System.out.println(result.get(n-1));
}

}

In C++

#include <iostream>

using namespace std;

int main()
{ int n;
cout<<“Enter the value for n\n”;
cin>>n;
cout<<“1″<<” “<<“1″<<” “<<“2″<<” “;
if(n==3)
return 100;
int a[100],x;
a[0]=1;a[1]=1;a[2]=2;
int i=3;
while(i<=n){
if((a[i-2])%2!=0){
x=a[i-2]*3;
a[i]=x;
}
else{
x=a[i-2]*2;
a[i]=x;
}
i++;
}i=3;
while(i<=n){
cout<<a[i]<<” “;
i++;
}

return 0;
}

Please comment code in other languages

Please Login/Signup to comment

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


    • 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


  • 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]);
      }