TCS Ninja Command Line Programming Questions
Command Line Programming Questions and Answers TCS Ninja
This section is very new in the TCS Syllabus and has only been introduced so you TCS only hires people who can write error free codes and help them build world class application. Also, non CS-IT students have been observed to perform badly in this section.
So we suggest for non coding branches to study command Line Programming theory from our TCS Ninja dashboard’s command line programming section first and then practice the questions below –
Following are graded by TCS Ninja online compiler while you’re solving a TCS Ninja command Line Programming Question –
a. Logical Correctness | b. Correct output at various inputs |
c. No compilation error | d. Correct output at various end points |
Command Line Program to Calculate the Square root of a function
#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]);
float i=0.00;
while(i*i<=n)
{
i=i+0.001;
}
i=i-0.001;
printf(“%.2f”,i);
}
}
Palindrome Number Command Line Programming
#include <stdio.h>
int main(int argc, char *argv[])
{
int num, reverse_num=0,remainder,temp;
num = atol(argv[1]);
temp=num;
while(temp!=0)
{
remainder=temp%10;
reverse_num=reverse_num*10+remainder;
temp/=10;
}
if(reverse_num==num)
printf(“%d is a palindrome number”,num);
else
printf(“%d is not a palindrome number”,num);
return 0;
}
sum of the digits of a number
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{ long num, temp, digit, sum = 0;
if(argc == 1 || argc > 2)
{
printf(“Enter the number\n”);
exit(1);
}
num = atoi (argv[1]) ;
temp = num;
while (num > 0) {
digit = num % 10;
sum = sum + digit;
num /= 10;
}
printf(“Sum of the digits of %ld = %ld\n”, temp, sum);
}
Reverse a Number
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
if(argc==1)
{
printf(“No Arguments”);
return 0;
}
else
{
int n,reverseNumber,temp,rem;
n=atoi(argv[1]);
temp=n;
reverseNumber=0;
while(temp)
{
rem=temp%10;
reverseNumber=reverseNumber*10+rem;
temp=temp/10;
}
printf(“%d”,reverseNumber);
return 0;
}
}
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);
}
Login/Signup to comment