TCS NQT 2018
SET-1 SET-2 SET-3 SET-4 Coding MCQs
[[The following questions are shared students who had taken the test. If you remembered any questions of any IT company please share with us in comment box]]Previously asked coding questions in TCS NQT for 2019 batch:
1)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 in 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 value of n th 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.
2)Consider the below 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 terms 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 in 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.
Source code:
#include<stdio.h>
#include<math.h>
int main()
{
int n,a,d,p,res;
scanf("%d",&n);
if(n%2!=0)// n is odd 0 2 4 6 8......
{
a=0;
d=2;
p=n/2+1;
}
else// n is even 0 1 2 3 4 5 6.....
{
a=0;
d=1;
p=n/2;
}
res=a+(p-1)*d;
printf("%d",res);
return 0;
}
3)Consider the below series :1 2 1 3 2 5 3 7 5 11 8 13 13 17 21 19 34 23 55 29 89 .....
This series is a mixture of 2 series all the odd terms in this series form Fibonacci sequence and even terms form prime numbers . Write a program to find the nth term in this series.
The value n in 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 11 and if n=13, the 13th term in the series is 13.
Only the value 13 should be printed to STDOUT.
4)The program will receive 3 English words inputs from STDIN
1.These three words will be read one at a time, in three separate lines.
2.The first word should be changed like all vowels should be replaced by $
3.The second word should be changed like all consonants should be replaced by #
4.The third word should be changed like all char should be converted to upper case
5.Then concatenate the three words and print them.
Ex: Input: how are you
output: h$wa##eYOU
Source code:
#include<stdio.h>
int main()
{
char s1[6],s2[6],s3[6];
int i;
fflush(stdin);
scanf("%s%s%s",s1,s2,s3);
for(i=0;s1[i]!='\0';i++)
{
if((s1[i]>='A'&&s1[i]<='Z')||(s1[i]>='a'&&
s1[i]<='z'))//s1[i] is alphabet
{
if(s1[i]=='a'||s1[i]=='e'||s1[i]=='i'||
s1[i]=='o'||s1[i]=='u'||s1[i]=='A'||
s1[i]=='E'||s1[i]=='I'||s1[i]=='O'||
s1[i]=='U')
s1[i]='$';
}//end of if
}//end of for loop
for(i=0;s2[i]!='\0';i++)
{
if((s2[i]>='A'&&s2[i]<='Z')||(s2[i]>='a'&&
s2[i]<='z'))//s2[i] is alphabet
{
if(s2[i]!='a'&&s2[i]!='e'&&s2[i]!='i'&&
s2[i]!='o'&&s2[i]!='u'&&s2[i]!='A'&&
s2[i]!='I'&&s2[i]!='O'&&s2[i]!='U'&&s2[i]!='E')
s2[i]='#';
}
}//end of for loop
for(i=0;s3[i]!='\0';i++)
{
if(s3[i]>='a'&&s3[i]<='z')//s3[i] is lower case alphabet
s3[i]=s3[i]+'A'-'a';//lower to upper case
}
printf("%s%s%s",s1,s2,s3);
return 0;
}
5)Given a string, convert all vowels to upper case and consonants to lower case.
Ex:if the string is TcsionNqt2018 then output is tcsIOnnqt2018
Source code:
#include<stdio.h>
int main()
{
char s[20];
int i;
scanf("%s",s);
for(i=0;s[i];i++)
{
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U')
{
if(s[i]>='a'&&s[i]<='z')
s[i]=s[i]+'A'-'a';
}
else
{
if(s[i]>='A'&&s[i]<='Z')
s[i]=s[i]+'a'-'A';
}
}
printf("%s",s);
return 0;
}
6)Given a string convert each character into its opposite case
Ex:Input: Tcs@Digital
Output:tCS@dIGITAL
Source code:Toggle case:
input: HeLLo
output:hEllO
input:Program
output:pROGRAM
input:SyNtAx1
output:sYnTaX1
input:@lphA
output:@LPHa
#include<stdio.h>
int main()
{
char s[50];
int i;
scanf("%s",s);
for(i=0;s[i];i++)
{
if(s[i]>='A'&&s[i]<='Z')
s[i]=s[i]+'a'-'A';
else
if(s[i]>='a'&&s[i]<='z')
s[i]=s[i]+'A'-'a';
}
printf("%s",s);
return 0;
}
String handling functions:
strlen()->non void .gives an int
strrev()->void.
strupr()->void
strlwr()->void
strcpy()->void
strcmp()->non void.returns 1 or 0 or -1
strstr()->non void.return type is char *
searches a substring in a given string
if substring found returns its address
if substring not found returns NULL
strcat()->void
*/
Previously asked coding questions in TCS:
- Program to check a given year is a leapyear or not
int main()
{
int year;
scanf("%d",&year);
if((year%4==0&&year%100!=0)||(year%400==0))
printf("Leapyear");
else
printf("Not leapyear");
return 0;
}
- Program to convent decimal number to binary
#include<stdio.h>
#include<string.h>
int main()
{
int i,rem;
long int n;
char res[100];
scanf("%ld",&n);
i=0;
while(n)
{
rem=n%2;
res[i++]=rem+'0';//to convent an integer to a character
n=n/2;
}
res[i]='\0';
strrev(res);
printf("%s",res);
return 0;
}
- Program to convent binary to decimal
Program to convent binary to decimal */
#include<stdio.h>
int main()
{
int n,p,res,rem;
scanf("%d",&n);
p=1;
res=0;
while(n)
{
rem=n%10;
res=res+rem*p;
n=n/10;
p=p*2;
}
printf("%d",res);
return 0;
}
input:101
output:5
input:11111111
output:255
#include<stdio.h>
int main()
{
int n,p,res,rem;
scanf("%d",&n);
p=1;
res=0;
while(n)
{
rem=n%10;
res=res+rem*p;
n=n/10;
p=p*2;
}
printf("%d",res);
return 0;
}
input:101
output:5
input:11111111
output:255
- program to convert an octal number to binary
binary and combined.
#include<stdio.h>
int main()
{
int rem,i;
char n[100];//to read octal input
char res[100]="";//to store output in binary
scanf("%s",n);//read input as a string
i=0;
while(n[i])//1st,2nd,3rd characters and so on
{
rem=n[i]-'0';//convert ith character into integer
switch(rem)
{
case 0:strcat(res,"000");
break;
case 1:strcat(res,"001");
break;
case 2:strcat(res,"010");
break;
case 3:strcat(res,"011");
break;
case 4:strcat(res,"100");
break;
case 5:strcat(res,"101");
break;
case 6:strcat(res,"110");
break;
case 7:strcat(res,"111");
break;
default:printf("Invalid number");
}
i++;
}
printf("%s",res);
return 0;
}
Input: 345
output: 011100101
Input: 120
int main()
{
int n1,n2,i,sum;
scanf("%d%d",&n1,&n2);
sum=0;
for(i=n1+1;i<n2;i++)
{
if(i%2)
sum=sum+i;
}
printf("%d",sum);
return 0;
}
input:2 10
output:24
#include<stdio.h>
int main()
{
int n1,n2,i,sum;
scanf("%d%d",&n1,&n2);
sum=0;
for(i=n1+1;i<n2;i++)
{
if(i%2==0)
sum=sum+i;
}
printf("%d",sum);
return 0;
}
input: 1 10
output:20
#include<stdio.h>
int main()
{
int n,i,flag;
scanf("%d",&n);
flag=0;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==0)
printf("prime");
else
printf("not prime");
return 0;
}
#include<stdio.h>
int main()
{
char s[30];
int len;
scanf("%s",s);
len=strlen(s);
printf("%c%c%c%c",s[0],s[1],s[len-2],s[len-1]);
return 0;
}
Input:TCSNQT
output:TCQT
Program to find factorial of a positive integer
program to find GCD and LCM of two positive integers
Find second maximum in a given array of elements.
#include<stdio.h>
#include<limits.h>
int main()
{
int a[30],n,i,max1,max2;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);//25 32 19 35 15
max1=INT_MIN;//25
max2=INT_MIN;
for(i=0;i<n;i++)
{
if(a[i]>max1)
{
max2=max1;
max1=a[i];
}
else
if(a[i]!=max1&&a[i]>max2)
max2=a[i];
}//end of for loop
if(max2==INT_MIN)
printf("NO second max");
else
printf("%d",max2);
return 0;
}
Input:
6
5 2 5 5 5 5
Input:
2
Input:
6
5 5 5 5 5 5
Input:
No second max
Rotate a given array in clock wise direction in a specified number of times.
ex:
Input: 1
4
10 12 15 17
Output:12 15 17 10
Input:3
7
2 3 4 5 6 7 8
Output:5 6 7 8 2 3 4
Source code:
#include<stdio.h>
int main()
{
int i,j,n,m,temp,a[50];
scanf("%d",&n);//no.of rotations
scanf("%d",&m);//array size
for(i=0;i<m;i++)
scanf("%d",&a[i]);
while(n--)
{
temp=a[0];
for(i=1;i<m;i++)
a[i-1]=a[i];
a[m-1]=temp;
}
for(i=0;i<m;i++)
printf("%d ",a[i]);
return 0;
}
Rotate a given array in anti-clock wise direction in a specified number of times.
ex:
Input: 1
4
10 12 15 17
Output:17 10 12 15
Input:3
7
2 3 4 5 6 7 8
Output:6 7 8 2 3 4 5
#include<stdio.h>
int main()
{
int i,j,n,m,temp,a[50];
scanf("%d",&n);//no.of rotations
scanf("%d",&m);//array size
for(i=0;i<m;i++)
scanf("%d",&a[i]);
while(n--)
{
temp=a[m-1];
for(i=m-2;i>=0;i--)
a[i+1]=a[i];
a[0]=temp;
}
for(i=0;i<m;i++)
printf("%d ",a[i]);
return 0;
}
Rotate a string in clock wise direction in a specified number of times.
ex:
Input: 1
Date
Output:ateD
Input:3
TCSNINJA
Output:NINJATCS
#include<stdio.h>
int main()
{
int i,j,n;
char s[40],temp,len;
scanf("%d",&n);
fflush(stdin);
scanf("%s",s);
len=strlen(s);//otherwise use this without missing semicolon for(len=0;s[len];len++);
while(n--)
{
temp=s[0];
for(i=1;i<len;i++)
s[i-1]=s[i];
s[len-1]=temp;
}
printf("%s",s);
return 0;
}
Rotate a string in anti clock wise direction in a specified number of times.
ex:
Input: 1
Date
Output:eDate
Input:3
TCSNINJA
Output:NJATCSNI
Source code:
#include<stdio.h>
int main()
{
int i,j,n;
char s[40],temp,len;
scanf("%d",&n);
fflush(stdin);
scanf("%s",s);
len=strlen(s);//otherwise use this without missing semicolon for(len=0;s[len];len++);
while(n--)
{
temp=s[len-1];
for(i=len-2;i>=0;i--)
s[i+1]=s[i];
s[0]=temp;
}
printf("%s",s);
return 0;
}
Remove vowels from a given string.
#include <stdio.h>
int main()
{
int i,j,len;
char s[100],*p,*t;
gets(s);
for(len=0;s[len];len++);
t=(char *)malloc(len);
j=0;
for(i=0;s[i];i++)
{
switch(s[i])
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':break; //character is vowel
default:t[j++]=s[i]; //not vowel
}
}
t[j]='\0' ;
printf("%s",t);
}
output: 001010000
- Program to find sum of odd number from N1 to N2 excluding N1 and N2
int main()
{
int n1,n2,i,sum;
scanf("%d%d",&n1,&n2);
sum=0;
for(i=n1+1;i<n2;i++)
{
if(i%2)
sum=sum+i;
}
printf("%d",sum);
return 0;
}
input:2 10
output:24
- Program to find even of odd number from N1 to N2 excluding N1 and N2
#include<stdio.h>
int main()
{
int n1,n2,i,sum;
scanf("%d%d",&n1,&n2);
sum=0;
for(i=n1+1;i<n2;i++)
{
if(i%2==0)
sum=sum+i;
}
printf("%d",sum);
return 0;
}
input: 1 10
output:20
- program to check a given number is prime or not?
#include<stdio.h>
int main()
{
int n,i,flag;
scanf("%d",&n);
flag=0;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==0)
printf("prime");
else
printf("not prime");
return 0;
}
#include<stdio.h>
int main()
{
char s[30];
int len;
scanf("%s",s);
len=strlen(s);
printf("%c%c%c%c",s[0],s[1],s[len-2],s[len-1]);
return 0;
}
Input:TCSNQT
output:TCQT
Program to find factorial of a positive integer
program to find GCD and LCM of two positive integers
Find second maximum in a given array of elements.
#include<stdio.h>
#include<limits.h>
int main()
{
int a[30],n,i,max1,max2;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);//25 32 19 35 15
max1=INT_MIN;//25
max2=INT_MIN;
for(i=0;i<n;i++)
{
if(a[i]>max1)
{
max2=max1;
max1=a[i];
}
else
if(a[i]!=max1&&a[i]>max2)
max2=a[i];
}//end of for loop
if(max2==INT_MIN)
printf("NO second max");
else
printf("%d",max2);
return 0;
}
Input:
5
25 32 19 35 15
Output:
32
Input:
6
5 2 5 5 5 5
Input:
2
Input:
6
5 5 5 5 5 5
Input:
No second max
Rotate a given array in clock wise direction in a specified number of times.
ex:
Input: 1
4
10 12 15 17
Output:12 15 17 10
Input:3
7
2 3 4 5 6 7 8
Output:5 6 7 8 2 3 4
Source code:
#include<stdio.h>
int main()
{
int i,j,n,m,temp,a[50];
scanf("%d",&n);//no.of rotations
scanf("%d",&m);//array size
for(i=0;i<m;i++)
scanf("%d",&a[i]);
while(n--)
{
temp=a[0];
for(i=1;i<m;i++)
a[i-1]=a[i];
a[m-1]=temp;
}
for(i=0;i<m;i++)
printf("%d ",a[i]);
return 0;
}
Rotate a given array in anti-clock wise direction in a specified number of times.
ex:
Input: 1
4
10 12 15 17
Output:17 10 12 15
Input:3
7
2 3 4 5 6 7 8
Output:6 7 8 2 3 4 5
#include<stdio.h>
int main()
{
int i,j,n,m,temp,a[50];
scanf("%d",&n);//no.of rotations
scanf("%d",&m);//array size
for(i=0;i<m;i++)
scanf("%d",&a[i]);
while(n--)
{
temp=a[m-1];
for(i=m-2;i>=0;i--)
a[i+1]=a[i];
a[0]=temp;
}
for(i=0;i<m;i++)
printf("%d ",a[i]);
return 0;
}
Rotate a string in clock wise direction in a specified number of times.
ex:
Input: 1
Date
Output:ateD
Input:3
TCSNINJA
Output:NINJATCS
#include<stdio.h>
int main()
{
int i,j,n;
char s[40],temp,len;
scanf("%d",&n);
fflush(stdin);
scanf("%s",s);
len=strlen(s);//otherwise use this without missing semicolon for(len=0;s[len];len++);
while(n--)
{
temp=s[0];
for(i=1;i<len;i++)
s[i-1]=s[i];
s[len-1]=temp;
}
printf("%s",s);
return 0;
}
Rotate a string in anti clock wise direction in a specified number of times.
ex:
Input: 1
Date
Output:eDate
Input:3
TCSNINJA
Output:NJATCSNI
Source code:
#include<stdio.h>
int main()
{
int i,j,n;
char s[40],temp,len;
scanf("%d",&n);
fflush(stdin);
scanf("%s",s);
len=strlen(s);//otherwise use this without missing semicolon for(len=0;s[len];len++);
while(n--)
{
temp=s[len-1];
for(i=len-2;i>=0;i--)
s[i+1]=s[i];
s[0]=temp;
}
printf("%s",s);
return 0;
}
Remove vowels from a given string.
#include <stdio.h>
int main()
{
int i,j,len;
char s[100],*p,*t;
gets(s);
for(len=0;s[len];len++);
t=(char *)malloc(len);
j=0;
for(i=0;s[i];i++)
{
switch(s[i])
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':break; //character is vowel
default:t[j++]=s[i]; //not vowel
}
}
t[j]='\0' ;
printf("%s",t);
}
Input: Elephant
Output: lphnt
Input: TCSNQT
Output: TCSNQT
Remove duplicates from a given string.
#include <stdio.h>
int main()
{
int i,j,k,len,f;
char s[100],*t;
gets(s);
for(len=0;s[len];len++);
t=(char *)malloc(len);
j=0;
for(i=0;s[i];i++)
{
f=0;
for(k=0;k<j;k++)
{
if(s[i]==t[k])
{
f=1;
break;
}
}
if(f==0)
t[j++]=s[i];
}
t[j]='\0' ;
printf("%s",t);
}
Input: TCSNQT
Output: TCSNQ
Input:abababab
Output: ab
Check two given strings Anagrams or not.
Note: two strings are said to anagrams, if they have same lengths and every character of first string should appear in second string not necessarily in same positions.
/*string anagram
SILENT
or
EINLST
Anagram
HELLO->EHLLO Anagrams
*/
#include<stdio.h>
int main()
{
char s1[100],s2[100];
int len1,len2,i,j,temp,f;
fflush(stdin);
scanf("%s",s1);
fflush(stdin);
scanf("%s",s2);
len1=strlen(s1);
len2=strlen(s2);
if(len1!=len2)
{
printf("Not Anagrams");
return 0;//program terminated
}
//sort both s1 and s2
for(i=0;i<len1-1;i++)
{
for(j=0;j<len1-1-i;j++)
{
if(s1[j]>s1[j+1])
{
temp=s1[j];
s1[j]=s1[j+1];
s1[j+1]=temp;
}
if(s2[j]>s2[j+1])
{
temp=s2[j];
s2[j]=s2[j+1];
s2[j+1]=temp;
}
}//end of for j loop
}//end of for i loop
//compare respective characters
f=0;
for(i=0;i<len1;i++)
{
if(s1[i]!=s2[i])
{
f=1;
break;
}
}
if(f==0)
printf("Anagram");
else
printf("Not Anagram");
return 0;
}
Output: lphnt
Input: TCSNQT
Output: TCSNQT
Remove duplicates from a given string.
#include <stdio.h>
int main()
{
int i,j,k,len,f;
char s[100],*t;
gets(s);
for(len=0;s[len];len++);
t=(char *)malloc(len);
j=0;
for(i=0;s[i];i++)
{
f=0;
for(k=0;k<j;k++)
{
if(s[i]==t[k])
{
f=1;
break;
}
}
if(f==0)
t[j++]=s[i];
}
t[j]='\0' ;
printf("%s",t);
}
Input: TCSNQT
Output: TCSNQ
Input:abababab
Output: ab
Check two given strings Anagrams or not.
Note: two strings are said to anagrams, if they have same lengths and every character of first string should appear in second string not necessarily in same positions.
/*string anagram
SILENT
or
EINLST
Anagram
HELLO->EHLLO Anagrams
*/
#include<stdio.h>
int main()
{
char s1[100],s2[100];
int len1,len2,i,j,temp,f;
fflush(stdin);
scanf("%s",s1);
fflush(stdin);
scanf("%s",s2);
len1=strlen(s1);
len2=strlen(s2);
if(len1!=len2)
{
printf("Not Anagrams");
return 0;//program terminated
}
//sort both s1 and s2
for(i=0;i<len1-1;i++)
{
for(j=0;j<len1-1-i;j++)
{
if(s1[j]>s1[j+1])
{
temp=s1[j];
s1[j]=s1[j+1];
s1[j+1]=temp;
}
if(s2[j]>s2[j+1])
{
temp=s2[j];
s2[j]=s2[j+1];
s2[j+1]=temp;
}
}//end of for j loop
}//end of for i loop
//compare respective characters
f=0;
for(i=0;i<len1;i++)
{
if(s1[i]!=s2[i])
{
f=1;
break;
}
}
if(f==0)
printf("Anagram");
else
printf("Not Anagram");
return 0;
}
No comments:
Post a Comment