0% found this document useful (0 votes)
464 views

TCS Previous Year Papers and Study Materials

The document provides information about TCS coding tests and command line programs questions. It discusses: 1) TCS uses command line programs for their first coding round placement tests, with 1 question in 20 minutes. Questions are easy and only partial or full output is required to pass. 2) Command line programs take arguments from the command line when run. The main function defines argc for argument count and argv which is an array of argument values. 3) Examples of common command line programs questions are provided, along with how to read arguments, convert to int, and print output without additional text.

Uploaded by

Praveen Kumar K
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
464 views

TCS Previous Year Papers and Study Materials

The document provides information about TCS coding tests and command line programs questions. It discusses: 1) TCS uses command line programs for their first coding round placement tests, with 1 question in 20 minutes. Questions are easy and only partial or full output is required to pass. 2) Command line programs take arguments from the command line when run. The main function defines argc for argument count and argv which is an array of argument values. 3) Examples of common command line programs questions are provided, along with how to read arguments, convert to int, and print output without additional text.

Uploaded by

Praveen Kumar K
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

 

TCS Coding Test

TCS Previous Year Papers 


and Study materials 

Department of
COMPUTER SCIENCE & ENGINEERING
 

Praveen Kumar. K, Sr. Asst. Prof.


Department of CSE
VR Siddhartha Engineering College
TCS Command Line Programs
TCS has been using TCS Command Line Programs to conduct the first round of
Placements in Coding Section.

TCS CODING ROUND QUESTIONS IN TEST PATTERN AND SYLLABUS


 Number of Questions – 1
 Total time to Solve – 20 mins
 Difficulty Level – 1 Easy Questions
 Cut-off – Solve 1 question completely or partial output.

Command Line Arguments Questions for TCS

For TCS Command Line Arguments Programs let us consider this, if you wanted to
write a basic C program then you would’ve written a main function that would’ve
looked like –

int main(){
// some code in Command Line Arguments Questions for TCS C Compiler
}}

However in command line arguments we write like this –

int main(int argc, char *argv[]){

1. argc – It is know as Argument Count and as clear from the name it stores the
Count of number of Arguments.
2. argv[] – Pointer, contains location of all the values(arguments).
3. *argv[] – Array of values of all the arguments.
4. They are parameters/arguments supplied to the program when it is invoked.

Thus, now we have two things for TCS C Compiler

1. Total Count of number of Arguments.


2. All the values/pointer location of arguments stored in an array.

Now, you will need one more thing i.e. atoi();

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

To pass command line arguments, we typically define main() with two arguments :
first argument is the number of command line arguments and second is list of
command-line arguments.

int main(int argc, char *argv[]) { /* ... */ }


or

int main(int argc, char **argv[]) { /* ... */ }

Quick Facts for TCS Command Line Arguments Programs

argv[0] contains the default value, not the input value so –


All for loops must start from i = 1.

You must use the following condition
if(argc == 1){
// do nothing since, there are no arguments, maybe ask for
arguments?
}else{
// code to apply logic and print values.
}
 provided+1 +1 for file.exe
 argv[argc] is a NULL pointer.
 argv[0] holds the name of the program.
 argv[1] points to the first command line argument and argv[n] points last
argument.

TCS Command Line No of Times asked in Difficulty Importance


Arguments Programs Command Line Level
Coding
Command Line Program for 25 High High
Binary to Decimal
TCS Command Line 21 Medium Medium
Argument for Factorial of a
Number
TCS Command Line 18 Medium Medium
Programs Armstrong
Number
To check if Prime number 27 Low Medium
or not
Sum of Digits of a Number 41 Medium Very High

Command Line Arguments Questions for TCS Rules for Coding Section Steps:
There is only one question for 20 minutes.

1. It has 10 attempts(We can compile only 10 times).


2. We must start our code from the scratch.
3. The coding platform is divided into two, one for writing the code and other for
output. We should write the whole program.
4. We can’t use any input functions like scanf(), getch(), getchar().
5. The input to be provided should be read as command line arguments.
Command Line Arguments Frequency of Repetition in Importance Difficulty
Questions for TCS TCS Questions
String Reversal Using Command 14 Low Very
Line Programming High
Average of two Numbers for TCS 39 Medium Medium
Finding LCM of two Numbers 51 High Medium
Finding HCF/GCD Question with 66 High Medium
CLP

We must only print exact output in Command Line Arguments Questions for TCS
Procedure –

1. Output must not be re-framed by extra words.


2. If there is any error, the error will be shown in the output dialog box.
3. The errors are clearly mentioned.
4. If there are no errors, a message like “compiled successfully” will be printed.
5. Along with that they will mention four test cases are ‘passed’ or maybe ‘failed’.
6. They are indicated like private and public test cases. They have not
mentioned what is the test case, which is difficult to understand in TCS
Command Line Arguments Programs.

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.

Ques. Sample Program to print all Integers using Command Line


Arguments?
This program is written for TCS interface.

// 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>

int main(int argc, char *argv[])


{
int a,b;
int i;
if(argc<2)
{
printf("please use \"prg_name value1 value2 ... \"\n");
return -1;
}

for(i=1; i<argc; i++)


{
printf("arg[%2d]: %d\n",i,atoi(argv[i]));
}
return 0;
}
Factorial of a Non Negative Integer
Problem Statement: Write a C program to calculate the factorial of a non-negative
integer N. The factorial of a number N is defined as the product of all integers from 1
up to N. Factorial of 0 is defined to be 1. The number N is a nonnegative integer that
will be passed to the program as the first command line parameter. Write the output
to stdout formatted as an integer WITHOUT any other additional text. You may
assume that the input integer will be such that the output will not exceed the largest
possible integer that can be stored in an int type variable.

Example:

If the argument is 4, the value of N is 4.


So, 4 factorial is 1*2*3*4 = 24.
Output : 24
The code below takes care of negative numbers but at the end of the
page there
is easier code which though doesn't take negative numbers in
consideration.
#include <stdio.h> // for printf
#include <stdlib.h> // for function atoi() for converting string into int
// Function to return fact value of n
int fact(int n)
{
if (n == 0)
return 1;
else {
int ans = 1;
int i;
for (i = 1; i <= n; i++) {
ans = ans * i;
}
return ans;
}
}
// argc tells the number of arguments
// provided+1 +1 for file.exe
// char *argv[] is used to store the
// command line arguments in the string format
int main(int argc, char* argv[])
{
// means only one argument exist that is file.exe
if (argc == 1) {
printf("No command line argument exist Please provide them first \n");
return 0;
} else {
int i, n, ans;
// actual arguments starts from index 1 to (argc-1)
for (i = 1; i < argc; i++) {
// function of stdlib.h to convert string
// into int using atoi() function
n = atoi(argv[i]);

// since we got the value of n as usual of


// input now perform operations
// on number which you have required

// get ans from function


ans = fact(n);

// print answer using stdio.h library's printf() function


printf("%d\n", ans);
}
return 0;
}
}

OutPut –

24 if command line argument is 4.

Area of a Triangle(Using Command Line Programming)


#include<stdio.h>
#include<math.h>
#include <stdlib.h>

int main(int argc, char *argv[])


{
if(argc==1)
{
printf(“No command line argument present, pls add them first”);
return 0;
}
else
{
int a, b, c;
float area,s;
a=atoi(argv[1]);
b=atoi(argv[2]);
c=atoi(argv[3]);
s=(a+b+c)/3;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf(“Area is %f”,area);
return 0;
}
}
Calculate length of the Hypotenuse of right angled triangle
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{

if(argc<2)
{
printf("please use \"prg_name value1 value2 ... \"\n");
return -1;
}

int a,b,side1,side2,side3;
a=atoi(argv[1]);
b=atoi(argv[2]);
side1=pow(a,2);
side2=pow(b,2);
side3=sqrt((side1+side2));
printf("the hypotenuse is %d",side3);
return 0;

Write a c program to check whether given no. is palindrome or not using


command line Arguments?

#include<stdio.h>
#include<stdlib.h>
int main(int argc, char* argv[])
{
int num=atoi(argv[1]);
if(isPalindrome(num))
printf("Palindrome");
else
printf("Not Palindrome");

return 0;
}
int isPalindrome(int n)
{
int m=n;
int rev=0;
while(m!=0)
{
rev=(rev*10) + (m%10);
m=m/10;
}
if(rev==n)
return 1;
else
return 0;
}

C program to find the sum of N prime number using Command Line


Arguments?
Prime Sum within given range
Write a C program that will find the sum of all prime numbers in a given range.The
range will be specified as command line parameters. The first command line
parameter, N1 which is a positive integer, will contain the lower bound of the range.
The second command line parameter N2, which is also a positive integer will the
upper bound of the range. The program should consider all the prime numbers within
the range, excluding the upper and lower bound. Print the output in integer format to
stdout. Other than the integer number, no other extra information should be printed to
stdout.

The Sum of Prime Numbers using Command Line Arguments in C

#include<stdio.h>
int main(int argc,char *argv[])
{
int N1,N2,i,j,sum=0,count,lower,upper;
if(argc!=3)
exit(0);
N1=atoi(argv[1]);
lower=N1+1;
N2=atoi(argv[2]);
upper=N2;
for(i=lower;i<upper;i++)
{
count=1;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
count++;
}
}
if(count==1)
{
sum=sum+i;
}
}
printf("%d",sum);
return 0;
}
String Reversal Program with Command Line Programming
It is highly advisable to go through Command Line Arguments before even
looking at the code. Here is the code for String Reversal Using Command Line
Arguments

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

int main(int argc, char *argv[])


{
int k;
char temp;
int i,j=0;
int strsize = 0;
for (i=1; i<argc; i++) {
strsize += strlen(argv[i]);
if (argc > i+1)
strsize++;
}
char *cmdstring;
cmdstring = malloc(strsize);
cmdstring[0] = '\0';
for (k=1; k<argc; k++) {
strcat(cmdstring, argv[k]);
if (argc > k+1)
strcat(cmdstring, " ");
}
i = 0;
j = strlen(cmdstring) - 1;
while (i < j) {
temp = cmdstring[i];
cmdstring[i] = cmdstring[j];
cmdstring[j] = temp;
i++;
j--;
}
printf("\nReverse string is :%s", cmdstring);
return(0);

Input – abcde

Output – edcba
Binary to decimal using Command Line arguments
#include <stdio.h>
int main(int argc, char *argv[])
{
int num,binary,decimal=0,rem,base=1;
num=atoi(argv[1]);
binary=num;
while(num>0)
{
rem=num%2;
decimal+=rem*base;
num=num/10;
base=base*2;
}
printf("%d",decimal);
return 0;
}

Command Line Program to Check if a String is Palindrome or Not


#include <stdio.h>
#include <string.h>

void isPalindrome(char str[])


{

int l = 0;
int h = strlen(str) - 1;

while (h > l)

if (str[l++] != str[h--])

printf("%s is Not Palindromen", str);

return;

}
}

printf("%s is palindromen", str);

int main(int argc, char *argv[])

int i,k;

int strsize = 0;

for (i=1; i<argc; i++) {

strsize += strlen(argv[i]);

if (argc > i+1)

strsize++;

char *cmdstring;

cmdstring = malloc(strsize);

cmdstring[0] = '\0';

for (k=1; k<argc; k++) {

strcat(cmdstring, argv[k]);

if (argc > k+1)

strcat(cmdstring, " ");

isPalindrome(cmdstring);

}
Using Command Line Arguments Program to Convert a Decimal to
Binary Number
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
if(argc==1)
{
printf("No Arguments ");
return 0;
}
else
{
int n;

n=atoi(argv[1]);
int binaryN[64];
int i=0;int j;
while(n>0)
{
//storing in binary array remainder of number
binaryN[i]=n%2;
n=n/2;
i++;
}
//printing reverse array
while(i)
{
printf("%d",binaryN[--i]);
}

return 0;
}
}

Area of a Triangle(Using Command Line Programming)


#include<stdio.h>
#include<math.h>
#include <stdlib.h>

int main(int argc, char *argv[])


{
if(argc==1)
{
printf(“No command line argument present, pls add them first”);
return 0;
}
else
{
int a, b, c;
float area,s;
a=atoi(argv[1]);
b=atoi(argv[2]);
c=atoi(argv[3]);
s=(a+b+c)/3;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf(“Area is %f”,area);
return 0;
}
}

You might also like