How to solve Command Line Arguments Coding Questions
TCS Command Line Programming Questions and Answers 2020-21
TCS has switched back to the command line programming for 2020 TCS NQT Exam. Last year this was excluded from the syllabus, but now they have included this topic again. You can expect one question from command line programming in the coding section
TCS Command Line Arguments Programs
This article is for TCS based command line Argument based Questions, you will get to know how to understand the the basic program and write it, Command Base Argument Coding for TCS, Command Line Arguments Questions for TCS.
TCS Coding Round | Details |
---|---|
Number of questions | 2 questions |
Time Alloted | 30 mins |
Difficulty level | High |
TCS CODING ROUND QUESTIONS IN TEST PATTERN AND SYLLABUS
- Number of Questions – 2
- Total time to Solve – 30 mins
- Difficulty Level – High
- Cut-off – Solve 1 question completely or partial output.
TCS Command Line Arguments Questions
- TCS Coding Questions – 1
- TCS Coding Questions – 2
- TCS Command Line Arguments – Fibonacci Series
- TCS Command Line Program to Swap two numbers
- TCS String Reversal Using Command Line Programming (Most Asked – 104 times)
- Greatest of Two Numbers using CLP
- LCM of Two Number using CLP
- Average of Two Numbers
- Sum of Digits of a number
- Binary to Decimal (asked 25 times)
- Decimal to Binary (asked 42 times)
- Nth term in Fibbonacci series
- Square Root of Prime Number
- Square Root without sqrt.h
- Armstrong Number
- Reverse Digits of a Number
Part 2
- Odd Even Number
- Binary to Octal
- Decimal to Octal
- Check Leap Year
- Area of Circle
- Area of Triangle
- Checking Palindrome (Number)
- Checking Palindrome (String) (asked 91 Times)
- Checking Prime or Not
Command Line Arguments Questions for TCS Basics
- Keywords like getc, scanf, getch, getchar etc can not be used.
- Instead we use command line arguments to fetch the values.
Before reading further, I will suggest not to freak out if you don’t understand TCS Command Line Arguments Programs 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 in TCS C Compiler.
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[]){
- argc – It is know 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.
Thus, now we have two things for TCS C Compiler
- Total Count of number of Arguments.
- 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.
[table id=101 /]
Command Line Arguments Questions for TCS Rules for Coding Section Steps:
There is only one question for 20 minutes.
- It has 10 attempts(We can compile only 10 times).
- We must start our code from the scratch.
- The coding platform is divided into two, one for writing the code and other for output. We should write the whole program.
- We can’t use any input functions like scanf(), getch(), getchar().
- The input to be provided should be read as command line arguments.
[table id=102 /]
We must only print exact output in Command Line Arguments Questions for TCS Procedure –
- Output must not be re-framed by extra words.
- If there is any error, the error will be shown in the output dialog box.
- The errors are clearly mentioned.
- If there are no errors, a message like “compiled successfully” will be printed.
- Along with that they will mention four test cases are ‘passed’ or maybe ‘failed’.
- 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.
Dont 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.
Most Asked Command Line Programs in TCS
You can scroll up to go to individual links of other asked programs but we first want you to learn command line programming above in the theory section and then come here to check most asked programs and then finally go to individual links to find other asked programs for command line.
Beware – Most websites are copying PrepInsta’s content/codes etc. Only trust us. If you see our copied content anywhere please contact us on Facebook and help us :).
Command Line Code for finding the Greatest Number
#include <stdio.h> int main (int argc, char *argv[]) { int c[10]; int i, temp, j, greatest; j = 0; for (i = 1; i < argc; i++) { temp = atoi (argv[i]); c[j] = temp; j++; } greatest = c[0]; for (i = 0; i < 10; i++) { if (c[i] > greatest) { greatest = c[i]; } } printf ("Greatest of ten numbers is %d", greatest); return 0; }
Command Line Code for Fibonacci Series
#include <stdio.h> int main (int argc, char *argv[]) { int n, first = 0, second = 1, next, c; n = atol (argv[1]); printf ("First %d terms of Fibonacci series are :-\n", n); for (c = 0; c < n; c++) { if (c <= 1) next = c; else { next = first + second; first = second; second = next; } printf ("%d\n", next); } return 0; }
Command Line Code for finding the Factorial of a number
#include<stdio.h> int main(int argc, char *argv[]) { int n,i; unsigned long long factorial = 1; n = atol(argv[1]); for(i=1; i<=n; ++i) { factorial *= i; } printf("Factorial of %d = %llu", n, factorial); }
Command Line Code for Reversal of a string
#include<stdio.h> #include<stdlib.h> #include<string.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); }
Command Line Code for Swapping of Numbers
#include<stdio.h> int main(int argc, char *argv[]) { double firstNumber, secondNumber, temporaryVariable; firstNumber = atol(argv[1]); secondNumber = atol(argv[2]); temporaryVariable = firstNumber; firstNumber = secondNumber; secondNumber = temporaryVariable; printf("\nAfter swapping, firstNumber = %.2lf\n", firstNumber); printf("After swapping, secondNumber = %.2lf", secondNumber); return 0; }
Loved this site.. Very helpful information. I have searched for this topic in different websites but didnt found this much data.
Great job PREPINSTA .. keep on doing
is TCS using Command Line Compiler in 2018 exams?
command line arguments