# TCS Coding Questions with Solutions For TCS Coding Test | FACE Prep

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

Before you start answering the below questions, have a look at

## TCS Coding questions – Important instructions

### Instructions

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.

### Points to note

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.

## Previous year TCS Coding Questions with Solutions

Numbers series coding questions are one of the types of questions that were asked in the recent TCS 2018 drive. Those coding questions will be of a mixture of two series where odd terms are of one kind and the even terms are of the other kind.

### TCS Coding Questions – 1

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 recent TCS drive 2018 which happened in the month of September 2018)

#include<stdio.h>
#define MAX 1000
void fibonacci(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)
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;
}

#include<iostream>
using namespace std;
#define MAX 1000

void fibonacci(int n)
{
int i, t1 = 0, t2 = 1, nextTerm;
for (i = 1; i<=n; i++)
{
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
cout << 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)
if(++count == n)
{
cout << i;
break;
}
}
}
int main()
{
int n;
cin >> n;
if(n%2 == 1)
fibonacci (n/2 + 1);
else
prime(n/2);
return 0;
}

import java.util.*;
public class Main
{
static void fibonacci(int n)
{
int i, t1 = 0, t2 = 1, nextTerm;
for (i = 1; i<=n; i++)
{
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
System.out.print(t1);
}
static void prime(int n)
{
int MAX = 1000;
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)
if(++count == n)
{
System.out.print(i);
break;
}
}
}
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
if(n%2 == 1)
fibonacci (n/2 + 1);
else
prime(n/2);
}
}

def fibonacci(n):
t1 = 0; t2 = 1
for i in range(1, n+1):
nextTerm = t1 + t2
t1 = t2
t2 = nextTerm
print(t1)

def prime(n):
MAX = 1000
count =0
for i in range(2, MAX):
flag = 0
for j in range(2, i):
if(i%j == 0):
flag = 1
break
if (flag == 0):
count = count + 1
if(count == n):
print(i)
break

n = int(input(“Enter the number : “))
if(n%2 == 1):
fibonacci (int(n/2) + 1)
else:
prime(int(n/2))

`Input: 14Output: 17`

### TCS Coding Questions – 2

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.

#include”stdio.h”
#include”math.h”
int main()
{
int input;
scanf(“%d”, &input);
if(input % 2 == 0)
{
int number = input/2;
int x = number – 1;
int output = pow(3, x);
printf(“%d”, output);
}
else
{
int number = (input+1)/2;
int x = number – 1;
int output = pow(2, x);
printf(“%d”, output);
}
return 0;
}

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int input;
cin >> input;
if(input % 2 == 0)
{
int number = input/2;
int x = number – 1;
int output = pow(3, x);
cout << output;
}
else
{
int number = (input+1)/2;
int x = number – 1;
int output = pow(2, x);
cout << output;
}
return 0;
}

import java.util.*;
public class Main
{
public static void main(String[] args)
{
int input;
Scanner sc = new Scanner(System.in);
input = sc.nextInt();
if(input % 2 == 0)
{
int number = input/2;
int x = number – 1;
int k = 3;
int output = (int)Math.pow(k, x);
System.out.print(output);
}
else
{
int number = (input+1)/2;
int x = number – 1;
int k = 2;
int output = (int)Math.pow(k, x);
System.out.print(output);
}
}
}

import math

inp = 16
if(inp % 2 == 0):
number = int(inp/2)
x = number – 1
k = 3
output = math.pow(k, x)
print(output)

else:
number = int((inp+1)/2)
x = number – 1
k = 2
output = math.pow(k, x)
print(output)

`Input:  16Output: 2187`

### TCS Coding Questions – 3

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 nth 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 nth 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.

#include “stdio.h”
#include “math.h”
int main()
{
int input;
scanf(“%d”, &input);
if(input % 2 == 0)
{
int number, output;
number = input/2;
int x = number -1;
output = x;
printf(“%d “, output);
}
else
{
int number, output;
number = (input+1)/2;
int x = number – 1;
output = 2 * x;
printf(“%d “, output);
}
return 0;
}

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int input;
cin >> input;
if(input % 2 == 0)
{
int number, output;
number = input/2;
int x = number -1;
output = x;
cout << output;
}
else
{
int number, output;
number = (input+1)/2;
int x = number – 1;
output = 2 * x;
cout << output;
}
return 0;
}

import java.util.*;
public class Main
{
public static void main(String[] args)
{
int input;
Scanner sc = new Scanner(System.in);
input = sc.nextInt();
if(input % 2 == 0)
{
int number = input/2;
int x = number – 1;
int k = 3;
int output = x;
System.out.print(output);
}
else
{
int number = (input+1)/2;
int x = number – 1;
int k = 2;
int output = 2*x;
System.out.print(output);
}
}
}

import math

inp = 10
if(inp % 2 == 0):
number = int(inp/2)
x = number – 1
k = 3
output = x
print(output)

else:
number = int((inp+1)/2)
x = number – 1
k = 2
output = 2*x
print(output)

`Input: 10Output: 4`

### TCS Coding Questions – 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 *
• In the third string, convert the lowercase letters to upper case.

#include <stdio.h>
#include <string.h>

int main()
{
char str, str1, str2;
scanf(“%s”, str);
scanf(“%s”, str1);
scanf(“%s”, str2);
int i;
for(i = 0; str[i] != ‘\0’; i++)
{
if(str[i] == ‘a’ || str[i] == ‘e’ || str[i] == ‘i’ || str[i] == ‘o’ || str[i] == ‘u’ || str[i] == ‘A’ || str[i] == ‘E’ || str[i] == ‘I’ || str[i] == ‘O’ || str[i] == ‘U’)
{
str[i] = ‘”‘;
}
}
for(i = 0; str1[i] != ‘\0’; i++)
{
if(str1[i] == ‘a’ || str1[i] == ‘e’ || str1[i] == ‘i’ || str1[i] == ‘o’ || str1[i] == ‘u’ || str1[i] == ‘A’ || str1[i] == ‘E’ || str1[i] == ‘I’ || str1[i] == ‘O’ || str1[i] == ‘U’)
{
continue;
}
else
str1[i] = ‘*’;
}

printf(“\nAfter Replacing : “);
printf(“\nString 1 : %s”, str);
printf(“\nString 2 : %s”, str1);
printf(“\nString 3 : %s\n”, strlwr(str2));

return 0;
}

#include <bits/stdc++.h>
#include <algorithm>
#include <string.h>
using namespace std;

int main()
{
char str, str1;
string s1;
cin >> str;
cin >> str1;
cin >> s1;
int i;
for(i = 0; str[i] != ‘\0’; i++)
{
if(str[i] == ‘a’ || str[i] == ‘e’ || str[i] == ‘i’ || str[i] == ‘o’ || str[i] == ‘u’ || str[i] == ‘A’ || str[i] == ‘E’ || str[i] == ‘I’ || str[i] == ‘O’ || str[i] == ‘U’)
{
str[i] = ‘”‘;
}
}
for(i = 0; str1[i] != ‘\0’; i++)
{
if(str1[i] == ‘a’ || str1[i] == ‘e’ || str1[i] == ‘i’ || str1[i] == ‘o’ || str1[i] == ‘u’ || str1[i] == ‘A’ || str1[i] == ‘E’ || str1[i] == ‘I’ || str1[i] == ‘O’ || str1[i] == ‘U’)
{
continue;
}
else
str1[i] = ‘*’;
}
transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
cout << “\nAfter replacing : “;
cout << “\nString 1 : ” << str << endl;
cout << “\nString 2 : ” << str1 << endl;
cout << “\nString 3 : ” << s1 << endl;

return 0;
}

import java.util.*;
public class Main
{
public static void main(String[] args)
{
String s1, s2, s3;
Scanner sc = new Scanner(System.in);
s1 = sc.next();
s2 = sc.next();
s3 = sc.next();
char[] str = s1. toCharArray();
char[] str1 = s2. toCharArray();
int i;
System.out.print(“\nAfter Replacing : \n”);

for(i = 0; i < str.length; i++)
{
if(str[i] == ‘a’ || str[i] == ‘e’ || str[i] == ‘i’ || str[i] == ‘o’ || str[i] == ‘u’ || str[i] == ‘A’ || str[i] == ‘E’ || str[i] == ‘I’ || str[i] == ‘O’ || str[i] == ‘U’)
{
s1 = s1.replace(str[i], ‘”‘);
}
}

for(i = 0; i < str1.length; i++)
{
if(str1[i] == ‘a’ || str1[i] == ‘e’ || str1[i] == ‘i’ || str1[i] == ‘o’ || str1[i] == ‘u’ || str1[i] == ‘A’ || str1[i] == ‘E’ || str1[i] == ‘I’ || str1[i] == ‘O’ || str1[i] == ‘U’)
{
continue;
}
else
s2 = s2.replace(str[i], ‘*’);
}

System.out.print(“\nString 1 : ” + s1);
System.out.print(“\nString 2 : ” + s2);
System.out.print(“\nString 3 : ” + s3.toLowerCase());

}
}

s = “Hello”
s2 = “Wonderful”
s3 = “Morning”
print(“Afetr replacing : “)
for i in range(len(s)):
if(s[i] == ‘a’ or s[i] == ‘e’ or s[i] == ‘i’ or s[i] == ‘o’ or s[i] == ‘u’ or s[i] == ‘A’ or s[i] == ‘E’ or s[i] == ‘I’ or s[i] == ‘O’ or s[i] == ‘U’):
s = s.replace(s[i], ‘”‘)
print(“String 1 : “,s)

for i in range(len(s1)):
if(s2[i] == ‘a’ or s2[i] == ‘e’ or s2[i] == ‘i’ or s2[i] == ‘o’ or s2[i] == ‘u’ or s2[i] == ‘A’ or s2[i] == ‘E’ or s2[i] == ‘I’ or s2[i] == ‘O’ or s2[i] == ‘U’):
continue
else:
s2 = s2.replace(s2[i],’*’)
print(“String 2 : “,s2)

print(“String 3 : “, s3.lower())

`Input:`

Hello
Good
Morning

Output:

H”ll”
*oo*
morning