TCS Coding Questions & Programming Round (Repeated)-2023
TCS Coding Questions & Programming Round (Repeated)-2023
C++
Java
Python
Perl
Placement Papers
IMPORTANT NOTE:
2. TCS NQT is non- adaptive this year though the TCS Coding Question round is different from this.
3. You will not get any extra rough paper in the exam as a calculator and Rough Paper will be available on your Desktop
Screen. You are not allowed to move your eyes down while giving the examination.
Clear TCS use Coupon Code “CT70“ and get flat 70% OFF on your Prime Subscription
manager wants to make the production of both types of vehicle according to the given data below:
The task is to find how many two-wheelers as well as four-wheelers need to manufacture as per the given data.
Example :
Input :
Output :
TW =130 FW=70
Explanation:
Constraints :
2<=W
W%2=0
V<W
Print “INVALID INPUT” , if inputs did not meet the constraints.
The candidate has to write the code to accept two positive numbers separated by a new line.
Written program code should generate two outputs, each separated by a single space character(see the example)
Additional messages in the output will result in the failure of test case
C++
Run
#include <bits/stdc++.h>
using namespace std;
int main ()
{
int v, w;
cin >> v >> w;
float x = ((4 * v) - w) / 2;
if ((w & 1) || w < 2 || w <= v)
{
cout << "INVALID INPUT";
return 0;
}
cout << "TW=" << x << " " << "FW=" << v - x;
Java
Python
the minimum number of ‘*’ or ‘#’ to make it a valid string. The string is considered valid if the number of ‘*’ and ‘#’ are equal.
Note : The output will be a positive or negative integer based on number of ‘*’ and ‘#’ in the input string.
Example 1:
Input 1:
Output :
C++
Run
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s="Hello";
int a=0,b=0;
getline(cin,s);
for(auto i:s)
if(i=='#')
a++;
else if(i=='*')
b++;
cout<<b-a;
}
Java
Python
elements.
Note : 1st element of the array should be considered in the count of the result.
For example,
Arr[]={7,4,8,2,9}
8 and 9 are also the elements that are greater than all of its previous elements.
Since total of 3 elements is present in the array that meets the condition.
Hence the output = 3.
Example 1:
Input
Output :
Example 2:
Output :
Constraints
1<=N<=20
1<=Arr[i]<=10000
C++
Java
Run
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;imax)
{
max=arr[i];
count++;
}
}
System.out.println(count);
}
}
Python
status (0/1) of a parking space is represented as the element of the matrix. The task is to find index of the prpeinzta
row(R) in the parking lot that has the most of the parking spaces full(1).
Note :
Example 1:
Input :
Output :
Example 2:
input :
Output :
C++
Run
#include <bits/stdc++.h>
using namespace std;
int main()
{
int r,c,a,sum=0,m=INT_MIN,in=0;
cin>>r>>c;
for(int i=0;i>a;
sum+=a;
}
if(sum>m)
{
m=sum;
in=i+1;
}
sum=0;
}
cout<< in;
}
Java
Python
TCS Menu
TCS Coding Question Day 2 Slot 1 – Question 1
PrepInsta A party has been organised on cruise. The party is organised for a limited time(T). The number of guests entering (E[i])
Home and leaving (L[i]) the party at every hour is represented as elements of the array. The task is to find the maximum
number of guests present on the cruise at any given instance within T hours.
Placement
Papers Example 1:
Input :
What Is
TCS NQT 5 -> Value of T
[7,0,5,1,3] -> E[], Element of E[0] to E[N-1], where input each element is separated by new line
Registration [1,2,1,3,4] -> L[], Element of L[0] to L[N-1], while input each element is separate by new line.
Process
Output :
Explanation:
1st hour:
PrepInsta Entry : 7 Exit: 1
Mock No. of guests on ship : 6
2nd hour :
Prime Entry : 0 Exit : 2
Video No. of guests on ship : 6-2=4
Hour 3:
How to use
PrepInsta Entry: 5 Exit: 1
Hour 4:
Entry : 1 Exit : 3
Entry : 3 Exit: 4
Example 2:
Input:
4 -> Value of T
[3,5,2,0] -> E[], Element of E[0] to E[N-1], where input each element is separated by new line.
[0,2,4,4] -> L[], Element of L[0] to L[N-1], while input each element in separated by new line
Output:
Cruise at an instance
Explanation:
Hour 1:
Entry: 3 Exit: 0
No. of guests on ship: 3
Hour 2:
Entry : 5 Exit : 2
No. of guest on ship: 3+5-2=6
Hour 3:
Entry : 2 Exit: 4
No. of guests on ship: 6+2-4= 4
Hour 4:
Entry: 0 Exit : 4
Second input- Accept T number of values, where each value is separated by a new line.
Third input- Accept T number of values, where each value is separated by a new line.
Constraints:
1<=T<=25
0<= E[i] <=500
0<= L[i] <=500
java
Run
import java.util.*;
class Main
{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int t = sc.nextInt ();
int e[] = new int[t];
int l[] = new int[t];
for (int i = 0; i < t; i++)
e[i] = sc.nextInt ();
The task is to find the colour (odd) of the balloon which is present odd number of times in the bunch of balloons.
Note: If there is more than one colour which is odd in number, then the first colour in the array which is present odd number
of times is displayed. The colours of the balloons can all be either upper case or lower case in the array. If all the inputs are
Example 1:
7 -> Value of N
[r,g,b,b,g,y,y] -> B[] Elements B[0] to B[N-1], where each input element is sepārated by ṉew line.
Output :
r -> [r,g,b,b,g,y,y] -> “r” colour balloon is present odd number of times in the bunch.
Explanation:
r: 1 balloon
g: 2 balloons
b: 2 balloons
y : 2 balloons
Hence , r is only the balloon which is odd in number.
Example 2:
Input:
10 -> Value of N
[a,b,b,b,c,c,c,a,f,c] -> B[], elements B[0] to B[N-1] where input each element is separated by new line.
Output :
b-> ‘b’ colour balloon is present odd number of times in the bunch.
Explanation:
a: 2 balloons
b: 3 balloons
c: 4 balloons
f: 1 balloons
Here, both ‘b’ and ‘f’ have odd number of balloons. But ‘b’ colour balloon occurs first.
Hence , b is the output.
The output should be a single literal (Check the output in example 1 and example 2)
Constraints:
3<=N<=50
B[i]={{a-z} or {A-Z}}
Java
Run
import java.util.*;
class Main
{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int n = sc.nextInt ();
char arr[] = new char[n];
for (int i = 0; i < n; i++)
arr[i] = sc.next ().charAt (0);
int lower[] = new int[26];
int upper[] = new int[26];
for (int i = 0; i < n; i++)
{
if ((arr[i] >= 'A') && (arr[i] <= 'Z'))
upper[arr[i] - 'A']++;
else if ((arr[i] >= 'a') && (arr[i] <= 'z'))
lower[arr[i] - 'a']++;
}
boolean flag = false;
char ch = '\0';
for (int i = 0; i < n; i++)
{
if ((arr[i] >= 'A') && (arr[i] <= 'Z'))
{
if (upper[arr[i] - 'A'] % 2 == 1)
{
ch = (char) (arr[i]);
flag = true;
break;
}
}
else if ((arr[i] >= 'a') && (arr[i] <= 'z'))
{
if (lower[arr[i] - 'a'] % 2 == 1)
{
ch = (char) (arr[i]);
flag = true;
break;
}
}
if (flag == true)
System.out.println (ch);
else
System.out.println ("All are even");
}
}
Question 1
T here is a JAR full of candies for sale at a mall counter. JAR has the capacity N, that is JAR can contain maximum N
candies when JAR is full. At any point of time. JAR can have M number of Candies where M<=N. Candies are served
to the customers. JAR is never remain empty as when last k candies are left. JAR if refilled with new candies in such a way
that JAR get full.
Write a code to implement above scenario. Display JAR at counter with available number of candies. Input should be the
number of candies one customer can order at point of time. Update the JAR after each purchase and display JAR at
Counter.
Output should give number of Candies sold and updated number of Candies in JAR.
If Input is more than candies in JAR, return: “INVALID INPUT”
Given,
Input Value
3
Output Value
Input Value
0
Output Value
Run
#include <stdio.h>
int main()
{
int n=10, k=5;
int num;
scanf("%d",&num);
if(num>=1 && num<=5)
{
printf("NUMBER OF CANDIES SOLD : %d\n",num);
printf("NUMBER OF CANDIES LEFT : %d",n-num);
}
else
{
printf("INVALID INPUT\n");
printf("NUMBER OF CANDIES LEFT : %d",n);
}
return 0;
}
C++
Java
Python
Question 2
Selection of MPCS exams include a fitness test which is conducted on ground. There will be a batch of 3 trainees, appearing
for running test in track for 3 rounds. You need to record their oxygen level after every round. After trainee are finished with
all rounds, calculate for each trainee his average oxygen level over the 3 rounds and select one with highest oxygen level as
the most fit trainee. If more than one trainee attains the same highest average level, they all need to be selected.
Display the most fit trainee (or trainees) and the highest average oxygen level.
Note:
The oxygen value entered should not be accepted if it is not in the range between 1 and 100.
If the calculated maximum average oxygen value of trainees is below 70 then declare the trainees as unfit with
meaningful message as “All trainees are unfit.
Average Oxygen Values should be rounded.
Example 1:
INPUT VALUES
95
92
95
92
90
92
90
92
90
OUTPUT VALUES
Trainee Number : 1
Trainee Number : 3
Note:
Round 1
Round 2
Round 3
Output must be in given format as in above example. For any wrong input final output should display “INVALID INPUT”
C
Run
#include <stdio.h>
int main()
{
int trainee[3][3];
int average[3] = {0};
int i, j, max=0;
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d",&trainee[i][j]);
if(trainee[i][j]<1 || trainee[i][j]>100)
{
trainee[i][j] = 0;
}
}
}
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
average[i] = average[i] + trainee[j][i];
}
average[i] = average[i] / 3;
}
for(i=0; i<3; i++) { if(average[i]>max)
{
max = average[i];
}
}
for(i=0; i<3; i++)
{
if(average[i]==max)
{
printf("Trainee Number : %d\n",i+1);
}
if(average[i]<70)
{
printf("Trainee is Unfit");
}
}
return 0;
}
C++
Java
Python
Question 3
Problem Statement
A washing machine works on the principle of Fuzzy System, the weight of clothes put inside it for washing is
uncertain But based on weight measured by sensors, it decides time and water level which can be changed by
For low level water, the time estimate is 25 minutes, where approximately weight is between 2000 grams or
any nonzero positive number below that.
For medium level water, the time estimate is 35 minutes, where approximately weight is between 2001 grams
For high level water, the time estimate is 45 minutes, where approximately weight is above 4000 grams.
Write a function which takes a numeric weight in the range [0,7000] as input and produces estimated time as
output is: “OVERLOADED”, and for all other inputs, the output statement is
“INVALID INPUT”.
Example:
Input value
2000
Output value
Time Estimated: 25 minutes
Solution
Run
#include <stdio.h>
void calculateTime(int n)
{
if(n==0)
printf("Time Estimated : 0 Minutes");
else if(n>0 && n<=2000)
printf("Time Estimated : 25 Minutes");
else if(n>2000 && n<=4000)
printf("Time Estimated : 35 Minutes");
else if(n>4000 && n<=7000)
printf("Time Estimated : 45 Minutes");
else
printf("INVALID INPUT");
}
int main()
{
int machineWeight;
scanf("%d",&machineWeight);
calculateTime(machineWeight);
return 0;
}
Python
C++
Problem Statement
The Caesar cipher is a type of substitution cipher in which each alphabet in the plaintext or messages is shifted by a
To pass an encrypted message from one person to another, it is first necessary that both parties have the ‘Key’ for the
cipher, so that the sender may encrypt and the receiver may decrypt it.
Key is the number of OFFSET to shift the cipher alphabet. Key can have basic shifts from 1 to 25 positions as there are 26
total alphabets.
As we are designing custom Caesar Cipher, in addition to alphabets, we are considering numeric digits from 0 to 9. Digits
For Example, if a given plain text contains any digit with values 5 and keyy =2, then 5 will be replaced by 7, “-”(minus sign)
will remain as it is. Key value less than 0 should result into “INVALID INPUT”
Example 1:
Enter your PlainText: All the best
Write a function CustomCaesarCipher(int key, String message) which will accept plaintext and key as input parameters and
C
Run
#include <stdio.h>
int main()
{
char str[100];
int key, i=0, left;
printf("Enter your plain text : ");
scanf("%[^\n]s",str);
printf("Enter the key : ");
scanf("%d",&key);
if(key==0)
{
printf("INVALID INPUT");
}
else
{
while(str[i]!='\0')
{
//printf("%d\n", str[i]);
if(str[i]>=48 && str[i]<=57)
{
if(str[i]+key<=57)
{
str[i] = str[i] + key;
}
else
{
left = (str[i] + key) - 57;
str[i] = 47 + left;
}
}
else if(str[i]>=65 && str[i]<=90)
{
if(str[i]+key<=90)
{
str[i] = str[i] + key;
}
else
{
left = (str[i] + key) - 90;
str[i] = 64 + left;
}
}
else if(str[i]>=97 && str[i]<=122)
{
if(str[i]+key<=122)
{
str[i] = str[i] + key;
}
else
{
left = (str[i] + key) - 122;
str[i] = 96 + left;
}
}
i++;
}
printf("The encrypted text is : %s",str);
}
return 0;
}
Python
C++
We want to estimate the cost of painting a property. Interior wall painting cost is Rs.18 per sq.ft. and exterior
Take input as
If a user enters zero as the number of walls then skip Surface area values as User may don’t want to paint
that wall.
Example 1:
3
12.3
15.2
12.3
15.2
12.3
15.2
10.10
10.10
10.00
C
Run
#include <stdio.h>
int main()
{
int ni,ne,i=0;
float int_p=18,ext_p=12,cost=0,temp;
scanf("%d %d",&ni,&ne);
if(ni<0 || ne<0 )
{
printf("INVALID INPUT");
}
else if(ni==0 && ne==0)
{
printf("Total estimated Cost : 0.0");
}
else
{
for(i=0;i<ni;i++)
{
scanf("%f",&temp);
cost+= int_p*temp;
}
for(i=0;i<ne;i++)
{
scanf("%f",&temp);
cost+= ext_p*temp;
}
printf("Total estimated Cost : %.1f",cost);
}
return 0;
}
C++
Python
JAVA
Question 6
Problem Statement
A City Bus is a Ring Route Bus which runs in circular fashion.That is, Bus once starts at the Source Bus Stop, halts at each
Bus Stop in its Route and at the end it reaches the Source Bus Stop again.
If there are n number of Stops and if the bus starts at Bus Stop 1, then after nth Bus Stop, the next stop in the Route will be
Fare is determined based on the distance covered from source to destination stop as Distance between Input Source and
Destination Stops can be measured by looking at values in array Path[] and fare can be calculated as per following criteria:
Path is circular in function. Value at each index indicates distance till current stop from the previous one. And each index
position can be mapped with values at same index in BusStops [] array, which is a string array holding abbreviation of
Write a code with function getFare(String Source, String Destination) which take Input as source and destination stops(in
the format containing first two characters of the Name of the Bus Stop) and calculate and return travel fare.
Example 1:
Input Values
ca
Ca
Output Values
INVALID OUTPUT
Example 2:
Input Values
NI
HA
Output Values
23.0 INR
Input should not be case sensitive and output should be in the format INR
C++
Run
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s , d;
cin>>s>>d;
transform(s.begin(),s.end() , s.begin(),::toupper);
transform(d.begin(),d.end() , d.begin(),::toupper);
string arrs[8] = {"TH" , "GA", "IC" , "HA" , "TE", "LU" ,"NI","CA"};
float arr[8]={800,600,750,900,1400,1200,1100,1500};
float res=0;
int st ,ed;
for(int i=0;i<8;i++)
{
if(s==arrs[i])
st=i;
if(d==arrs[i])
ed=i;
}
if(st==ed)
{
cout<< " INVALID INPUT";
return 0;
}
else
{
int i=st+1;
cout<< i;
while(i!=ed+1)
{
res+=(arr[i]);
i=(i+1)%8;
}
cout<<(ceil)(res*0.005);
return 0;
}
}
Python
Question 7
Problem Statement
There are total n number of Monkeys sitting on the branches of a huge Tree. As travelers offer Bananas and Peanuts, the
Monkeys jump down the Tree. If every Monkey can eat k Bananas and j Peanuts. If total m number of Bananas and p
number of Peanuts are offered by travelers, calculate how many Monkeys remain on the Tree after some of them jumped
down to eat.
At a time one Monkeys gets down and finishes eating and go to the other side of the road. The Monkey who climbed down
does not climb up again after eating until the other Monkeys finish eating.
Monkey can either eat k Bananas or j Peanuts. If for last Monkey there are less than k Bananas left on the ground or less
than j Peanuts left on the ground, only that Monkey can eat Bananas(<k) along with the Peanuts(<j).
Write code to take inputs as n, m, p, k, j and return the number of Monkeys left on the Tree.
Where, n= Total no of Monkeys
k= Number of eatable Bananas by Single Monkey (Monkey that jumped down last may get less than k Bananas)
j = Number of eatable Peanuts by single Monkey(Monkey that jumped down last may get less than j Peanuts)
Remember that the Monkeys always eat Bananas and Peanuts, so there is no possibility of k and j having a value zero
Example 1:
Input Values
20
12
12
Output Values
Note: Kindly follow the order of inputs as n,k,j,m,p as given in the above example. And output must include the same
C
Run
#include <stdio.h>
int main ()
{
int n, k, j, m, p;
float atebanana = 0.0, atepeanut = 0.0;
scanf ("%d %d %d %d %d", &n, &k, &j, &m, &p);
if (n < 0 || k < 0 || j < 0 || m < 0 || p < 0)
{
printf ("INVALID INPUT");
}
else
{
if (k > 0)
{
atebanana = (float) (m / k);
m = m % k;
}
if (j > 0)
{
atepeanut = (float) (p / j);
p = p % j;
}
n = n - atebanana - atepeanut;
if ((m != 0) || (p != 0))
n = n - 1;
printf ("Number of Monkeys left on the Tree:%d", n);
}
return 0;
}
JAVA
C++
Question 8
Problem Statement
Chain Marketing Organization has has a scheme for income generation, through which its members generate income for
themselves. The scheme is such that suppose A joins the scheme and makes R and V to join this scheme then A is Parent
Member of R and V who are child Members. When any member joins the scheme then the parent gets total commission of
members in the scheme. The fixed amount for joining the scheme is Rs.5000 on which commission will be generated
SchemeAmount = 5000
COMISSION DETAILS
Amit: 1000 INR
Amit
Y
Rajesh
Output:
Total Members: 2
Comission Details
Amit: 500 INR
C++
Run
#include<bits/stdc++.h>
using namespace std;
int main()
{
string par;
cin >> par;
string x;
cin >> x;
if (x == "N") {
cout << "TOTAL MEMBERS:1\n";
cout << "COMISSION DETAILS\n";
cout << par << ":250 INR\n";
} else {
string child;
cin >> child;
vectorv;
string temp = "";
for (int i = 0; i < child.length(); i++) {
if (child[i] == ',') {
v.push_back(temp);
temp = "";
}
else if (child[i] != ' ')
temp += child[i];
}
v.push_back(temp);
cout << "TOTAL MEMBERS:" << v.size() + 1 << "\n";
cout << "COMISSION DETAILS\n";
cout << par << ":" << v.size() * 500 << " INR\n";
for (auto a : v) {
cout << a << ":" << "250 INR\n";
}
}
}
Python
Question 9
Problem Statement
FULLY AUTOMATIC VENDING MACHINE – dispenses your cuppa on just press of button. A vending machine can serve
range of products as follows:
Coffee
1. Espresso Coffee
2. Cappuccino Coffee
3. Latte Coffee
Tea
1. Plain Tea
2. Assam Tea
3. Ginger Tea
4. Cardamom Tea
5. Masala Tea
6. Lemon Tea
7. Green Tea
8. Organic Darjeeling Tea
Soups
Beverages
Write a program to take input for main menu & sub menu and display the name of sub menu selected in the following
format (enter the first letter to select main menu):
Welcome to CCD
Enjoy your
Example 1:
Input:
c
Output
Welcome to CCD!
Enjoy your Espresso Coffee!
Example 2:
Input:
t
9
Output
INVALID OUTPUT!
Run
#include <stdio.h>
int main()
{
char c[3][20]={"Espresso Coffee","Cappuccino Coffee","Latte Coffee"};
char t[8][30]=
{"Plain Tea","Assam Tea","Ginger Tea","Cardamom Tea","Masala Tea","Lemon Tea","Green T
char s[4][20]=
{"Hot and Sour Soup","Veg Corn Soup","Tomato Soup","Spicy Tomato Soup"};
char ch;
int item, i;
scanf("%c",&ch);
scanf("%d",&item);
if(ch=='c')
if(item==i+1)
break;
if(i==3)
printf("INVALID OPTION!");
else if(ch=='t')
if(item==i+1)
break;
if(i==8)
printf("INVALID OPTION!");
}
else if(ch=='s')
if(item==i+1)
break;
if(i==4)
printf("INVALID OPTION!");
else if(ch=='b')
if(item==i+1)
break;
if(i==3)
printf("INVALID OPTION!");
else
printf("INVALID INPUT!");
return 0;
}
Python
C++
Question 10
Problem Statement
A doctor has a clinic where he serves his patients. The doctor’s consultation fees are different for different groups of
patients depending on their age. If the patient’s age is below 17, fees is 200 INR. If the patient’s age is between 17 and 40,
fees is 400 INR. If patient’s age is above 40, fees is 300 INR. Write a code to calculate earnings in a day for which one
array/List of values representing age of patients visited on that day is passed as input.
Note:
Example 1:
Input
20
30
40
50
14
Output
Note: Input and Output Format should be same as given in the above example.
Python
Run
age = []
for i in range(20):
m = input()
if m == "":
break
elif int(m) in range(0,120):
age.append(int(m))
else:
print("INVALID INPUT")
exit()
fees = 0
for i in age:
if i < 17:
fees+=200
elif i <40:
fees+=400
else:
fees+=300
print("Total Income {} INR".format(fees))
C++
Explanation:
Step 1:
Step 2:
Step 3:
Run
#include <stdio.h>
int leapprog(int year)
{
//checking divisibility by 4
if(year%4 == 0)
{
//checking divisibility by 100
if( year%100 == 0)
{
//checking divisibility by 400
if ( year%400 == 0)
printf("%d, the year entered happens to be a leap year", year);
else
printf("%d is surely not a leap year", year);
}
else
printf("%d, the year entered happens to be a leap year", year );
}
else
printf("%d is surely not a leap year", year);
return 0;
}
int main()
{
int input_year, val;
printf("Enter the year that you want to check"); //enter the year to check
scanf("%d",&input_year);
val = leapprog(input_year);
return 0;
}
C++
Java
Python
Perl
Prime Numbers with a Twist
Ques. Write a code to check whether no is prime or not. Condition use function check() to find whether entered no is
positive or negative ,if negative then enter the no, And if yes pas no as a parameter to prime() and check whether no is
prime or not?
Whether the number is positive or not, if it is negative then print the message “please enter the positive number”
It is positive then call the function prime and check whether the take positive number is prime or not.
Run
#include <stdio.h>
void prime(int n)
{
int c=0;
for(int i=2;i<n;i++) { if(n%i==0) c = c+1; }
if(c>=1)
printf("%d is not a prime number",n);
else
printf("%d is a prime number",n);
}
void main()
{
int n;
printf("Enter no : "); //enter the number
scanf("%d",&n);
if(n<0)
{
printf("Please enter a positive integer");
}
else
prime(n);
}
C++
Java
Python
Perl
0,0,7,6,14,12,21,18, 28
Explanation :
In this series the odd term is increment of 7 {0, 7, 14, 21, 28, 35 – – – – – – }
And even term is a increment of 6 {0, 6, 12, 18, 24, 30 – – – – – – }
C
Run
#include <stdio.h>
int main()
{
int i, n, a=0, b=0;
printf("enter number : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2!=0)
{
a = a + 7;
}
else
{
b = b + 6;
}
}
if(n%2!=0)
{
printf("%d term of series is %d\t",n,a-7);
}
else
{
printf("%d term of series is %d\t",n,b-6);
}
return 0;
}
C++
Java
Python
Perl
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.
Run
#include <stdio.h>
int main()
{
int i, n, a=1, b=1;
printf("enter number : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2!=0)
{
a = a * 2;
}
else
{
b = b * 3;
}
}
if(n%2!=0)
{
printf("\n%d term of series is %d\t",n,a/2);
}
else
{
printf("\n%d term of series is %d\t",n,b/3);
}
return 0;
}
C++
Java
Python
Perl
terms is derived from the previous term using the formula (x/2)
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
C
Run
#include <stdio.h>
int main()
{
int i, n, a=0, b=0;
printf("enter number : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2!=0)
{
if(i>1)
a = a + 2;
}
else
{
b = a/2;
}
}
if(n%2!=0)
{
printf("%d",a);
}
else
{
printf("%d",b);
}
return 0;
}
C++
Java
Python
1. These three words will be read one at a time, in three separate line
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
Other than these concatenated word, no other characters/string should or message should be written to STDOUT
For example if you print how are you then output should be h%wa#eYOU.
You can assume that input of each word will not exceed more than 5 chars
(Asked 25 times) TCS Ninja Coding Questions from Previous Years (TCS Ninja)
C
Run
#include <stdio.h>
#include
int main()
{
char a[10], b[10], c[10];
int i,j;
int x, y, z;
scanf("%s",a);
scanf("%s",b);
scanf("%s",c);
x = strlen(a);
y = strlen(b);
for(i=0;i<x;i++)
{
if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u')
{
a[i] = '%';
}
}
for(j=0;j<y;j++)
{
if(b[j]=='b'||b[j]=='c'||b[j]=='d'||b[j]=='f'||b[j]=='g'||b[j]=='h'||b[j]=='j'||b[j]==
b[j]=='m'||b[j]=='n'||b[j]=='p'||b[j]=='q'||b[j]=='r'||b[j]=='s'||b[j]=='t'||b[j]=='v
b[j]=='x'||b[j]=='y'||b[j]=='z')
{
b[j] = '#';
}
if(b[j]=='B'||b[j]=='C'||b[j]=='D'||b[j]=='F'||b[j]=='G'||b[j]=='H'||b[j]=='J'||b[j]==
b[j]=='M'||b[j]=='N'||b[j]=='P'||b[j]=='Q'||b[j]=='R'||b[j]=='S'||b[j]=='T'||b[j]=='V
b[j]=='X'||b[j]=='Y'||b[j]=='Z')
{
b[j] = '#';
}
}
z=0;
while (c[z] != '\0') {
if (c[z] >= 'a' && c[z] <= 'z')
{
c[z] = c[z] - 32;
}
z++;
}
printf("%s%s%s",a,b,c);
}
C++
Java
Using a method, pass two variables and find the sum of two numbers.
Test case:
Number 1 – 20
Number 2 – 20.38
Sum = 40.38
There were a total of 4 test cases. Once you compile 3 of them will be shown to you and 1 will be a hidden one. You have to
Run
#include <stdio.h>
addition(int x, float y)
{
float ans;
ans = (float)x + y;
printf("Answer : %.2f",ans);
}
int main()
{
int a;
float b;
printf("enter first number : ");
scanf("%d",&a);
printf("enter second number : ");
scanf("%f",&b);
addition(a, b);
}
C++
Java
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
C
Run
Code:
#include <stdio.h>
int main() {
//code
int n;
scanf(“%d”, &n);
if(n % 2 == 1)
{
int a = 1;
int r = 2;
int term_in_series = (n+1)/2;
int res = 2 * (term_in_series – 1);
printf(“%d “, res);
}
else
{
int a = 1;
int r = 3;
int term_in_series = n/2;
return 0;
}
C++
Free Materials
We have a lots of free materials don’t worry. Check them out below.
Once finished with this Coding section, visit our Main TCS Dashboard here for other sections like Aptitude, Coding
Questions, Command Line Programming etc.
TCS Coding Section used to be based on Command Line Argument based Coding Questions it is available
no where else on the internet. We will try help you with learning command line argument based coding for
TCS. From this year there is no command Line programming but you have to use C, C++, Java, Python and
Perl. We have the latest set of questions for the new pattern Question 0 Find the nth term of the series.
1,1,2,3,4,9,8,27,16,81,32,243,….
Run
#include <stdio.h>
#include <string.h>
int three(n)
int x,i;
for(i=0;i<100;i++)
x=pow(3,i);
if(i==n)
printf("%d",x);
int two(n)
int x,i;
for(i=0;i<100;i++)
x=pow(2,i);
if(i==n)
printf("%d",x);
int main()
int n;
scanf("%d",&n);
if(n%2==0)
three(n/2);
else
two(n/2+1);
Prime MOCK: Study Your TCS Coding Latest Prime MOCK Here
The platform will be eclipse based compiler. You can code on onlinegdb.com. All your codes working on this website
would run perfectly in TCS compiler.
Though command Line Programming is not there you should study it for C MCQ section as 1-2 MCQ would be there for
Before reading further for TCS Coding Round Questions, I will suggest not to freak out if you don’t understand in first go.
Read everything once and then when you read again things will start to make sense. It is the best resource on the internet to
know about TCS command line Argument Type Questions and TCS C Programming Questions and Answers.
[table id=76 /]
int Main(){
// some code
}}
argc – It is known as Argument Count and as clear from the name it stores the Count of number of Arguments.
argv[] – Pointer, contains location of all the values(arguments).
*argv[] – Array of values of all the arguments.
They are parameters/arguments supplied to the program when it is invoked.
atoi(); – Converts string into int and atoi(argv[i]); will give the value of argument at ith location in int type format.
Now you can use an int val = atoi(argv[i]); to store the value and then print it with printf(); function.
Check out this video Below to learn how to Solve Command Line Programming.
(Most Asked) TCS NQT Coding Questions with Answers (TCS NQT 2020)
Quick Facts
if(argc == 1){
}else{
// code to apply logic and print values in TCS Coding Round Questions.
[table id=103 /]
Command Line Arguments here – Read this properly.
Run
// Program to print all value of
// command line argument
// once we get the value from command
// line we can use them to solve our problem.
#include <stdio.h>
// this is used for function atoi() for converting string into int
else {
int i;
// actual arguments starts from index 1 to (argc-1)
for (i = 1; i < argc; i++) {
int value = atoi(argv[i]);
// print value using stdio.h library's printf() function
printf("%d\n", value);
}
return 0;
}
}
TCS Coding Questions
FAQ's
Don’t Compile again and again since compiler takes 25 seconds and each time you compile 25 seconds will
become lesser in the time you have to code in TCS Placement Papers Programming.
How to clear the TCS coding round?
Ans. First you must learn command line programming from our coding dashboard and then try sample
questions given on the dashboard to know how to clear the tcs coding round and TCS C Programming
What are some other websites where we can find more questions?
Ans. You can find more questions on MyGeekMonkey website.
Disclaimer: The Words Like “Placement Papers” and “Previous Year Papers” are used here
for Google Search Purposes only and may not be. All these questions could be freely
available on the internet, we are only charging students for the PrepInsta’s Mock Test
experiences and Analytics as well as preparation for the exam. Prepinsta does not
guarantee any recurrence of the questions in the exam however we believe that from our
practise questions the exam should atleast be similar in pattern as per syllabus or difficulty.
These are only practise mock questions. PrepInsta has compiled these from various internet
sources and made them as per mock experience for students ease and are offering
analytics as per performance
Get Prime
Course List
Checkout list of all the video courses in PrepInsta Prime Subscription
Checkout
Logged in as Gideon. Edit your profile. Log out? Required fields are marked *
« Previous 1 … 3 4 5
No.1 and most visited website for Privacy Policy VMware Oracle HirePro Dashboard Youtube a.com
by Toppers on PrepInsta.
54