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
C
C++
Java
Python
C
#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; }
C++
#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; }
Java
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
Python
num = int(input())
if(num%2==0):
num = num // 2
print(3**(num-1))
else:
num = num // 2 + 1
print(2**(num-1))
Login/Signup to comment
#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;
}
#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…………….
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);
}
}
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))
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);
}
Wow
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
//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;
}
i want this progrom in python
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
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])
I want explanation
#include
using namespace std;
void gp_two(int n)
{
int a=1;
bool x=true;
while(n–)
{
if(x)x=false;
else a*=2;
}
cout<<a;
}
void gp_three(int n)
{
int a=1;
bool x=true;
while(n–)
{
if(x)x=false;
else a*=3;
}
cout<>n;
if(n&1)gp_two(n/2+1);
else gp_three(n/2);
}
code is self explanatory
i need series logic !!
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….
#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;
}
#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]);
}
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;
}