TCS NQT - Coding Ability

Test Number 11


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

 

Input #1:

 

defer

 

Output:

 

defer is a keyword

 

Input #2: While

Output: while is not a keyword

       

#include<iostream>
#include<string.h>

using namespace std;
int main(){

char str[16][10] = {"break", "case", "continue", "default", "defer", "else","for",
"func", "goto", "if", "map", "range", "return", "struct", "type", "var"};
char input[20];
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;
}

     

import java.util.Scanner;
class Main
{
public static void main(String args[])
{

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

int flag = 0;
Scanner sc = new Scanner(System.in);
String input=sc.nextLine();

for(int i = 0; i<16;i++){

if(str[i].equals(input)){
flag = 1;
break;
}
}

if(flag==1){
System.out.println(input+" is a keyword");
}
else{
System.out.println(input+" is not a keyword");
}

}
}

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

input_var = input()
if input_var in keyword:
print(input_var+ " is a keyword")
else:
print(input_var+ " is not a keyword")





";

2.Given a pair of positive integers m and n (m < n; 0 < m < 999; 1 < n < = 999), write a program to smartly affix zeroes, while printing the numbers from m to n.

 

Example-1

Input

5 10

Expected output

05 06 07 08 09 10

 

Example-2

Input

9 100

Expected output

009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100

 

Example-3

Input: 1 9

Output: 1 2 3 4 5 6 7 8 9

int main()
{
int up,low;
cin >> low >> up;
for(int i=low; i<=up; i++)
{
if(up>=100)
printf("%03d ",i);
else if(up>=10)
printf("%02d ",i);
else
printf("%d ",i);
}
return 0;
}

import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int low=sc.nextInt();
int up=sc.nextInt();
for(int i=low;i<=up;i++)
{
if(up>=100)
System.out.printf("%03d ",i);
else if(up>=10)
System.out.printf("%02d ",i);
else
System.out.printf("%d ",i);
}
}
}

low=int(input())
up=int(input())
for i in range(low,up+1):
if(up>=100):
print("{:03d}".format(i),end=" ")
elif(up>=10):
print("{:02d}".format(i),end=" ")
else:
print(i,end=" ")