Take FREE!! Online Mettl Mock Test to Crack TechM and Other Companies Written Exams.
Get Off-Campus Placement Jobs Info !!!
TCS NQT Technical and Managerial Round Interview Questions

Mixture series

Home > Programs > Basics

210 / 265

Consider the following 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.

Show Full Paragraph

Answer:

#include

int main() {
//code int n;
scanf("%d", &n);
if(n % 2 == 1)
{
int a = 1; int r = 2;
int term_in_series = (n+1)/2;
int res = 2 * (term_in_series - 1);
printf("%d ", res);
}
else
{
int a = 1; int r = 3;
int term_in_series = n/2;
int res = term_in_series - 1;
printf("%d ", res);
}
return 0;
}

Asked In :: TCS NQT Wipro NLTH

Post Your Answer Here:

Language:

Post Your Reply Here: