Link copied to clipboard. Share away!

Dissmis

Published on 07 Mar 2020

TCS coding questions are a must to prepare for the TCS exam. Here all the previously asked TCS coding questions were discussed with the detailed answers. TCS Coding questions are based on the number series and basic programs.

You can also check out these before you start preparing for the coding section

- TCS Aptitude Questions
- TCS Verbal Question
- TCS Programming Questions
- TCS Off campus drive Important dates

Numbers series Coding questions are one of the types of question that were asked in the recent TCS 2018 drive. Those coding questions will be of a mixture of two series where odd terms of one kind and the even terms of the other kind. For Example, consider the given series: 1, 2, 1, 3, 2, 5, 3, 7, 5, 11, 8, 13, 13, 17,

This series is a mixture of 2 series all the odd terms in this series form a Fibonacci series and all the even terms are the prime numbers in ascending order. Now write a program to find the Nth term in this series.

The above coding question was asked in the TCS drive 2018 which happened in the month of September 2018.

```
#include"stdio.h"
#define MAX 1000
voidfibonacci(int n)
{
int i, t1 = 0, t2 = 1, nextTerm;
for (i = 1; i<=n; i++)
{
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
printf("%d", t1);
}
void prime(int n)
{
int i, j, flag, count =0;
for (i=2; i<=MAX; i++)
{
flag = 0;
for (j=2; j<i; j++)
{
if(i%j == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
count++;
if(count == n)
{
printf("%d", i);
break;
}
}
}
int main( )
{
int n;
scanf("%d", &n);
if(n%2 == 1)
fibonacci (n/2 + 1);
else
prime(n/2);
return 0;
}
```

**Input:**14

**Output:**17

1) Only One question, 20 minutes.

2) Choice of C / C++ / Java / Perl / Python 2.7.

3) Provided an IDE to debug.

4) For Java, the class name should be named Maze.

5)The input to the program either through STDIN / Command line arguments, as per the instructions.

6)The program should write the output to STDOUT.

7) Public and private test cases based evaluation.

1) While printing the output no leading or trailing spaces should be printed.

2) Other than the required output, no other text should be printed.

3) If the output is a number, no leading sign must be printed unless it is a negative number.

4) No scientific notation (3.9265E + 2).

5) All floating point numbers must contain that many decimal places as mentioned in the question.

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 is 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 the value of the nth 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.

**Answer:**

```
#include"stdio.h"
#include"math.h"
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 = pow(2, term_in_series - 1);
printf("%d ", res);
}
else
{
int a = 1;
int r = 3;
int term_in_series = n/2;
int res = pow(3, term_in_series - 1);
printf("%d ", res);
}
return 0;
}
```

**Input:** 16

**Output: **2187

Consider the following series: 0,0,2,1,4,2,6,3,8,4,10,5,12,6,14,7,16,8

This series is a mixture of 2 series all the odd terms in this series form even numbers in ascending order and every even term is derived from the previous term using the formula (x/2).

Write a program to find the n^{th }term in this series.

The value n is 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 the value of the n^{th }term no other characters /strings or message should be written to STDOUT.

For example, if n=10, the 10^{th} term in the series is to be derived from the 9th term in the series. The 9th term is 8 so the 10th term is (8/2)=4. Only the value 4 should be printed to STDOUT. You can assume that the 'n' will not exceed 20,000.

**Answer:**

```
#include "stdio.h"
#include "math.h"
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;
}
```

**Input: **10

**Output: **4

Consider the given input and output:

**Input: **Get 3 strings in 3 lines as input

Hello

Hi

Good Morning

**Output:**

In the 1st string, replace the vowels with '$', in the 2nd string replace the consonants with '#' and for the 3rd string replace the upper case into lower and lower case into upper. And print these converted string in a single line as output.

H $ll$ #i gOOD mORNING

**Answer:**

```
#include "stdio.h"
#include "string.h"
int main()
{
char str1[20], str2[20], str3[20], ans3[20];
int i;
scanf("%s", str1);
scanf("%s", str2);
scanf("%s", str3);
for(i=0; i<=strlen(str1); i++)
{
if(check_vowel(str1[i]) == 1)
str1[i] = '$';
}
printf("%s", str1);
for(i=0; str2[i]!=''; i++)
{
if((str2[i] =='a'||str2[i]=='e'||str2[i]=='i'||str2[i]=='o'||str2[i]=='u' || str2[i] == 'A' || str2[i] == 'E' || str2[i] == 'I' || str2[i] == 'O' || str2[i] == 'U') == 0)
{
str2[i] = '#';
}
}
printf("%s", str2);
for(i=0;i<=strlen(str3);i++)
{
if(str3[i]>='a' && str3[i]<='z')
{
ans3[i]=str3[i]-32;
}
if(str3[i]>='A' && str3[i]<='Z')
{
ans3[i]=str3[i]+32;
}
}
ans3[--i] = '';
printf("%s", ans3);
return 0;
}
```

**Input: **heyheygOOd

**Output: **h$y#e#GooD

Practice more TCS Coding questions

If you have any feedback about this
article and want to improve this, please write to **enquiry@faceprep.in**

×