##
**Tcs NQT 2020 exam - coding section solutions for all slots : **

**NOTE: GitHub source code available for all questions in C, C++ and in python programming languages click here :**

**SLOT: 1**

**NOTE:**

*c, c+ +, java, Perl, python 2.7*

**Problem statement**

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 keyword or not**

**Example-1**

Input: defer

Expected Output: defer is a keyword

**Example-2**

Input: While

Expected Output: while is not a keyword

**PROGRAM IN C++ :**

#include <iostream> using namespace std; int main() { string data; cin>>data; if(data == "break" || data == "case"

||data == "continue" ||data == "default"

||data == "defer" ||data == "else"

||data == "for" ||data == "func"

||data == "goto" ||data == "if"

||data == "map" ||data == "range"

||data == "return" ||data == "struct" ||data == "type" ||data == "var") cout<<data<<" is a keyword"; else cout<<data<<" is not a keyword"; return 0; }

**SLOT: 2**

**Write a program to find the difference between the sum of the digits in the even places and sum of the digits in the odd places**

**Problem description**

int the number 5179

odd place terms are : (5+7) = 12

even place terms are : (1+9) = 10

result : 12 - 10 = 2

odd place terms are : (5+7) = 12

even place terms are : (1+9) = 10

result : 12 - 10 = 2

**Example-1**

Input: 5179

Expected Output: 2

**Example-2**

Input: 718

Expected Output: 14

**PROGRAM IN C++ :**

#include <iostream> #include <cstring> using namespace std; int main() { int number; cin>>number; int even = 0 , odd = 0 ;

int checker = 1 , result = 0; while(number != 0) { if (checker % 2 == 0) even += number % 10; else odd += number % 10; number /= 10; checker++; } result = even - odd; if( result > 0) cout<<result; else cout<< -1 * result; return 0; }

**SLOT: 3**

**Write a program to convert a decimal number in the base 17 to a decimal number in base 10 :**

**Problem statement**

apply decimal conversion logic to solve this problem, where

*0 - 9*digits are same, while replace*A B C D E F G*with*10 11 12 13 14 15 16*respectively in the given input and do the conversion accordingly

**Example-1**

Input: 1A

Expected Output: 27

**Example-2**

Input: 23GF

Expected Output: 10980

here position starts at ( 0,0 ) and it will move in the direction right, top, left , bottom at the multiples of 10 . ( consider a graph with origin and directions as x, y, -x, -y on moving in x, y it will be positive term and in -x and - y it will be in negative terms )

**PROGRAM IN C++ :**

#include <iostream> #include <cstring> using namespace std; int val(char c) { if (c >= '0' && c <= '9') return (int)c - '0'; else return (int)c - 'A' + 10; } int toDeci(char *str) {

int base = 17 ; int len = strlen(str); int power = 1; int num = 0; int i; for (i = len - 1; i >= 0; i--) { num += val(str[i]) * power; power = power * base; } return num; } int main() { char str[50]; cin>>str; cout<<toDeci(str); return 0; }

**SLOT: 4**

**Write a program to find the nth value in the series.**

**Problem description**

here position starts at ( 0,0 ) and it will move in the direction right, top, left , bottom at the multiples of 10 . ( consider a graph with origin and directions as x, y, -x, -y on moving in x, y it will be positive term and in -x and - y it will be in negative terms )

**Example-1**

Input: 1

Expected Output: (10,0)

**Example-2**

Input: 4

Expected Output: (-20 , -20 )

**Example-3**

Input: 3

Expected Output: (-20 , 20 )

**PROGRAM IN C++ :**

#include <iostream> using namespace std; int values(int n){ int x =0 , y = 0 , rem = 0; for (int i = 0 ; i < n ; i++) { rem = i % 4; if (rem == 0) x = x + 10*(i+1); else if (rem == 1) y = y + 10*(i+1); else if (rem == 2) x = x - 10*(i+1); else if (rem == 3) y = y - 10*(i+1); } cout<<x<<","<<y; } int main() { int number; cin>>number; values(number); return 0; }

#include

ReplyDeleteusing namespace std;

void values(int n){ //change the function with return type void

int x =0 , y = 0 , rem = 0;

for (int i = 0 ; i < n ; i++)

{

rem = i % 4;

if (rem == 0)

x = x + 10*(i+1);

else if (rem == 1)

y = y + 10*(i+1);

else if (rem == 2)

x = x - 10*(i+1);

else if (rem == 3)

y = y - 10*(i+1);

}

cout<>number;

values(number);

return 0;

}