Prime #### Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime #### Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

# TCS NQT Coding Questions and Answers in TCS NQT Test

## TCS NQT Coding Questions for 2020 Batch

TC NQT coding questions and answers are discussed on this page below, all of these questions are actual questions asked in TCS NQT exams.

TCS NQT exam generally has the following types of questions –

• Pattern Programming
• Number Series
• Arithmetic Operations
• Strings
• Pattern Matching
• Logical Analysis ## TCS NQT Coding Question 0

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

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 Nth term, no other characters/strings or message should be written to STDOUT.

For example, when N = 14, the 14th term in the series is 17. So only the value 17 should be printed to STDOUT. ## Test Cases

Case 1

• Input: 14
• Expected Output: 17

Case 1

• Input: 5
• Expected Output: 2

### Solution

You can read the below posts to understand how program below works –

`#include<stdio.h>#define MAX 99999void fibonacci(int n){    /* Variable initialization */    int a = 0, b = 1, next;    //the below code is for fibonacci series till nth position    for (int i = 1; i<=n; i++)    {        next = a + b;        a = b;        b = next;    }    //will print a not b or next as they are stored to calculate next  and next to next term    printf("%d", a);}void prime(int n){    int i, j, flag, count =0;    //as prime numbers in given question start from 2    for (i=2; i<=MAX; i++)    {        flag = 0;        //to check if divisible apart from 1 & itself        //loop starts from 2 to ignore divisibilty by 1 & ends before the number itself        for (j=2; j<i; j++)        {            if(i%j == 0)            {                //number is not prime                flag = 1;                break;            }        }        //is prime        if (flag == 0){            //if found the nth prime number            if(++count == n)            {                printf("%d", i);                break;            }        }    }}int main(){    int n;    scanf("%d", &n);        /*if n is odd        nth number in main series will be found at (n/2 + 1) position         in fibonacci sub series    else         if n is even then it will be found in (n/2) position in prime sub series */        if(n%2 == 1)         fibonacci (n/2 + 1);    else         prime(n/2);            return 0;}`
`#include<iostream>#define MAX 99999using namespace std;void fibonacci(int n){    /* Variable initialization */    int a = 0, b = 1, next;    //the below code is for fibonacci series till nth position    for (int i = 1; i<=n; i++)    {        next = a + b;        a = b;        b = next;    }    //will print a not b or next as they are stored to calculate next  and next to next term    cout<< a;}void prime(int n){    int i, j, flag, count =0;    //as prime numbers in given question start from 2    for (i=2; i<=MAX; i++)    {        flag = 0;        //to check if divisible apart from 1 & itself        //loop starts from 2 to ignore divisibilty by 1 & ends before the number itself        for (j=2; j<i; j++)        {            if(i%j == 0)            {                //number is not prime                flag = 1;                break;            }        }        //is prime        if (flag == 0){            //if found the nth prime number            if(++count == n)            {                cout<< i;                break;            }        }    }}int main(){    int n;    cin >> n;        /*if n is odd        nth number in main series will be found at (n/2 + 1) position         in fibonacci sub series    else         if n is even then it will be found in (n/2) position in prime sub series */        if(n%2 == 1)         fibonacci (n/2 + 1);    else         prime(n/2);            return 0;}`

## TCS NQT Coding Question 1

### abbcccddddeeeeeff…..

Print the above series, take input from the user which will be used to print the series till the nth term

Assume the input will never exceed its value more than 100

Case 1

• Input – 25
• Expected Output – abbcccddddeeeeeffffffgggg

Case 2

• Input – 34
• Expected Output – abbcccddddeeeeeffffffggggggghhhhhh
```#include <stdio.h>

int main()
{
int n,count = 0,val=97;
scanf("%d",&n);

//for loop with no condition 2 (as using break to loop out of for loop)
for(int i = 0; ; i++)
{

//j<=i will take care of a bb ccc dddd printing
for(int j = 0;j<=i;j++){

//val is 97 so a is printed as a's ascii value is 65 val++ happens outside of loop
printf("%c",val);

//keeping track of total alphabets printed
count++;

//coming out of first for loop as soon as count >=n
if(count>=n)
break;
}
//incrementing alphabets ascii codes
val++;

//coming out of second for loop as soon as count >=n
if(count>=n)
break;
}
return 0;
}```
```#include <iostream>
using namespace std;
int main()
{
int n,count = 0,val=97;
cin >> n;

//for loop with no condition 2 (as using break to loop out of for loop)
for(int i = 0; ; i++)
{

//j<=i will take care of a bb ccc dddd printing
for(int j = 0;j<=i;j++){

//val is 97 so a is printed as a's ascii value is 65 val++ happens outside of loop
cout << (char) val;

//keeping track of total alphabets printed
count++;

//coming out of first for loop as soon as count >=n
if(count>=n)
break;
}
//incrementing alphabets ascii codes
val++;

//coming out of second for loop as soon as count >=n
if(count>=n)
break;
}
return 0;
}```

## TCS NQT Coding Question 2

### 1 1 2 2 4 4 8 8 16 16 …

Given a series whose even term creates a separate geometric series and odd term creates another geometric series.

Take the following input from the user –

• Total terms to be printed
• Common difference r1 for odd terms
• Common difference r2 for even terms

Ask the user for the number of terms and print the whole series.

### Test Cases

#### Case 1

• Input-
• 10
• 2
• 2
• Expected Output : 1 1 2 2 4 4 8 8 16 16

#### Case 2

• Input-
• 10
• 3
• 4
• Expected Output : 1 1 3 4 9 16 27 64 81 256
`#include <stdio.h>int main(){    int num, r1, r2;        printf("nth value : ");    scanf("%d", &num);    printf("Enter the common ratio for GP - 1 : ");    scanf("%d", &r1);    printf("nEnter the common ratio for GP - 2 : ");    scanf("%d", &r2);    int GP1 = 1, GP2 = 1;    for(int i = 0; i < num/2; i++)    {        printf("%d ", GP1);        GP1 = GP1 * r1;        printf("%d ", GP2);        GP2 = GP2 * r2;    }        //GP 1 will be printed 1 extra time then GP 2 if numbers of terms are odd    if(num%2 != 0)        printf("%d ", GP1);    return 0;}`
`#include <iostream>using namespace std;int main(){    int num, r1, r2;        cout << "nth value : ";    cin >> num;    cout << "Enter the common ratio for GP - 1 : ";    cin >> r1;    cout << "nEnter the common ratio for GP - 2 : ";    cin >> r2;    int GP1 = 1, GP2 = 1;    for(int i = 0; i < num/2; i++)    {        cout << " " <<  GP1;        GP1 = GP1 * r1;        cout << " " << GP2;        GP2 = GP2 * r2;    }        //GP 1 will be printed 1 extra time then GP 2 if numbers of terms are odd    if(num%2 != 0)        cout << " " << GP1;    return 0;}`

## TCS NQT Coding Question 3

Word is the Key 💖 (Link to this question)

(TCS NQT – Aug 2019 Slot 1)

One programming language has the following keywords that cannot be used as identifiers:

break, case, continue, default, defer, else, for, func, goto, if, map, range, return, struct, type, var

Write a program to find if the given word is a keyword or not

### Test cases

#### Case 1

• Input – defer
• Expected Output – defer is a keyword

#### Case 2

• Input – While
• Expected Output – while is not a keyword
```#include<stdio.h>
#include<string.h>

int main(){

char str = {"break", "case", "continue", "default", "defer", "else","for",
"func", "goto", "if", "map", "range", "return", "struct", "type", "var"};

char input;

int flag = 0;
scanf("%s",input);

for(int i = 0; i<16;i++){
if(strcmp(input,str[i]) == 0){
flag = 1;
break;
}
}

if(flag==1){
printf("%s is a keyword",input);
}
else{
printf("%s is not a keyword",input);
}
return 0;
}```
```#include<iostream>
#include<string.h>
using namespace std;
int main(){

char str = {"break", "case", "continue", "default", "defer", "else","for",
"func", "goto", "if", "map", "range", "return", "struct", "type", "var"};

char input;

int flag = 0;
cin >> input;

for(int i = 0; i<16;i++){
if(strcmp(input,str[i]) == 0){
flag = 1;
break;
}
}

if(flag==1){
cout << input << " is a keyword";
}
else{
cout << input << " is not a keyword";
}
return 0;
}```

## TCS NQT Coding Question 4

Sweet Seventeen 💖 (Link to this question)

(TCS NQT – Aug 2019 Slot 2)

Given a maximum of four digit to the base 17 (10 – A, 11 – B, 12 – C, 13 – D … 16 – G} as input, output its decimal value.

### Test Cases

#### Case 1

• Input – 1A
• Expected Output – 27

#### Case 2

• Input – 23GF
• Expected Output – 10980
```#include <stdio.h>
#include <math.h>
#include <string.h>

int main(){

char hex;
long long decimal, place;

int i = 0, val, len;
decimal = 0;
place = 1;

scanf("%s",hex);

len = strlen(hex);
len--;

for(i = 0;hex[i]!='\0';i++)
{
if(hex[i]>='0'&& hex[i]<='9'){

//48 to 57 are ascii values of 0 - 9
//say value is 8 its ascii will be 56
//val = hex[i] - 48 => 56 - 48 => val = 8

val = hex[i] - 48;
}
else if(hex[i]>='a'&& hex[i]<='g'){

//97 to 103 are ascii values of a - g
//say value is g its ascii will be 103
//val = hex[i] - 97 + 10 => 103 - 97 + 10=> val = 16
//10 is added as g value is 16 not 6 or a value is 10 not 0

val = hex[i] - 97 + 10;
}
else if(hex[i]>='A'&& hex[i]<='G'){

//similarly, 65 to 71 are values of A - G
val = hex[i] - 65 + 10;
}

decimal = decimal + val * pow(17,len);
len--;
}

printf("%lld",decimal);

return 0;
}```
```#include <iostream>
#include <math.h>
#include <string.h>

using namespace std;
int main(){

char hex;
long long decimal, place;

int i = 0, val, len;
decimal = 0;
place = 1;

cin>> hex;

len = strlen(hex);
len--;

for(i = 0;hex[i]!='\0';i++)
{
if(hex[i]>='0'&& hex[i]<='9'){

//48 to 57 are ascii values of 0 - 9
//say value is 8 its ascii will be 56
//val = hex[i] - 48 => 56 - 48 => val = 8

val = hex[i] - 48;
}
else if(hex[i]>='a'&& hex[i]<='g'){

//97 to 103 are ascii values of a - g
//say value is g its ascii will be 103
//val = hex[i] - 97 + 10 => 103 - 97 + 10=> val = 16
//10 is added as g value is 16 not 6 or a value is 10 not 0

val = hex[i] - 97 + 10;
}
else if(hex[i]>='A'&& hex[i]<='G'){

//similarly, 65 to 71 are values of A - G
val = hex[i] - 65 + 10;
}

decimal = decimal + val * pow(17,len);
len--;
}

cout<< decimal;

return 0;
}```

TCS NQT Coding Question 5

The Oddly even 💖 (Link to this question)

(TCS NQT – Aug 2019 Slot 3)

## Problem

Given a maximum of 100 digit numbers as input, find the difference between the sum of odd and even position digits

### Test Cases

#### Case 1

• Input: 4567
• Expected Output: 2

Explanation : Odd positions are 4 and 6 as they are pos: 1 and pos: 3, both have sum 10. Similarly, 5 and 7 are at even positions pos: 2 and pos: 4 with sum 12. Thus, difference is 12 – 10 = 2

#### Case 2

• Input: 5476
• Expected Output: 2

#### Case 3

• Input: 9834698765123
• Expected Output: 2
```#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
int a = 0,b = 0,i = 0, n;
char num;

printf("Enter the number:");
scanf("%s",num);    //get the input up to 100 digit
n = strlen(num);
while(n>0)
{
if(i==0)         //add even digits when no of digit is even and vise versa
{
a+=num[n-1]-48;
n--;
i=1;
}
else            //add odd digits when no of digit is even and vice versa
{
b+=num[n-1]-48;
n--;
i=0;
}
}
printf("%d",abs(a-b)); //print the difference of odd and even

return 0;
}```
```#include <iostream>
#include <string.h>
#include <stdlib.h>using namespace std;

int main()
{
int a = 0,b = 0,i = 0, n;
char num;

cout<< "Enter the number:";
cin>> num;    //get the input up to 100 digit
n = strlen(num);
while(n>0)
{
if(i==0)         //add even digits when no of digit is even and vise versa
{
a+=num[n-1]-48;
n--;
i=1;
}
else            //add odd digits when no of digit is even and vice versa
{
b+=num[n-1]-48;
n--;
i=0;
}
}
cout<< abs(a-b); //print the difference of odd and even

return 0;
}```

## Other Practise Questions important for TCS NQT  • Highest Common Factor(HCF): C | C++Java
• Lowest Common Multiple (LCM) : C | C++ | Java
• Greatest Common Divisor : C | C++ | Java
• Binary to Decimal to conversion : C | C++ | Java
• Binary to Octal conversion : C | C++ | Java
• Decimal to Binary conversion: C | C++ | Java
• Decimal to octal Conversion: C | C++ | Java
• Octal to Binary conversion : C | C++ | Java
• Octal to Decimal conversion : C | C++ | Java
• Quadrants in which a given coordinate lies : C | C++ | Java
• Permutations in which n people can occupy r seats in a classroom : C | C++ | Java
• Maximum number of handshakes: C | C++ | Java
• Addition of two fractions: C | C++ | Java
• Replace all 0’s with 1 in a given integer : C | C++ | Java
• Can a number be expressed as a sum of two prime numbers
: C | C++ | Java