ar7eo%8 ‘TOS Ninja coding questions and answers -
TCS Ninja coding questions and answers
TCS Ninja coding questions are a must to prepare to clear the TCS Ninja test. We are going to
discuss all the previously asked TCS Ninja coding questions in this article. These will help you
prepare for any kind of coding questions asked in the exam.
TCS Ninja Mock test questions - Coding section
Consider the below series:
1,2,1,3,2,5,3,7,5, 11,8, 13, 13, 17,...
This series is a mixture of 2 series ~ all the odd terms in this series form a Fibonacci series and
all the even terms are the prime numbers in ascending order.
Write a program to find the Nth term in this series.
The value N is 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 Nth term,
no other characters/strings or message should be written to STDOUT.
For example, when N = 14, the 14th term in the series is 17. So only the value 17 should be
printed to STDOUT.
Program:
htps:ww faceprep.infes-ina-coding-questions! 1ar7eo%8 ‘TOS Ninja coding questions and answers -
#include
int i,t1=0,t2 = 1,nextTerm;
for (i= 1; i<=n; i++)
{
nextTerm = t1 + t2;
t1=t2;
t2 = nextTerm;
}
printf("9%d", t1);
}
void prime(int n)
{
inti, j, flag, count =O;
for (i=2; i<=MAX; i+)
if (flag
count+#;
if(count == n)
{
printf("%d", i);
break;
}
htps:ww faceprep.infes-ina-coding-questions! anaar7eo%8 ‘TOS Ninja coding questions and answers -
}
int n;
scanf(“%d", &n);
if (n%2 == 1)
fibonacei (n/2 + 1);
else
prime(n/2);
return 0;
}
TCS Ninja Coding question 1:
Factorial program in c using command line arguments.
Explanation: Factorial of a non-negative integer n, denoted by n!, is the product of all positive
integers less than or equal to n. For example, The value of 5! is 5*4*3"2"1 = 120
Solution:
#include
int main(int a, char *b{]) //command line arguments
{
int xy
x=atoi(b[1]); //atoi function is to convert a character to integer
for(i=1ji<=x;i+-+)
i
fefti;
}
printf(*s6df);
return 0;
]
htps:ww faceprep.infes-ina-coding-questions! anaar7eo%8 ‘TOS Ninja coding questions and answers -
TCS Ninja Coding question 2:
variable with 2 point precision.
Solutio
#include
#define P| 3.14
int main(int a, char *b[]) //command line arguments
{
int d; float area =0;
d= atoilargv[1));
area =(float) PI*(d/2)"(d/2);
printf("%0.2f", area); //%0.2f is to print the answer with 2 values after decimal point.
return 0;
}
TCS Ninja Coding question 3:
Write ac program, to check whether the given year is a leap year or not using command line
arguments. A leap year is a calendar year containing one additional day (Feb 29th) added to
keep the calendar year synchronized with the astronomical year.
Solutio
#include
int main(int a, char*bl])
{
int year; year=atoi(b[1));
iflyear%100==0)
{
iflyear%400--0)
htps:ww faceprep.infes-ina-coding-questions! anaar7eo%8 ‘TOS Ninja coding questions and answers -
{
printf("NOT LEAP YEAR"); }}
else if(year%4==0)
{
printf("LEAP YEAR");
3
elsef
printf("NOT LEAP YEAR");
}
return 0;}
TCS Ninja Coding question 4:
Write ac program, to find the GCD of the given 2 numbers, using command line arguments
The input is 2 integer and the output GCD also should be an integer value.
Solution:
#include
int main(int x, char *y[])
{
inta,b,small,i
a-atoi(yl1));
beatoi(y[2]);
small=a>b2b:a;
for(i=smalli:
printf("9d.i);
break;
htps:ww faceprep.infes-ina-coding-questions! onaar7eo%8 ‘TOS Ninja coding questions and answers -
a
TCS Ninja Coding question 5:
C Program to check whether a given number is a prime number or not. The given number N, a
positive integer, will be passed to the program using the first command line parameter. Ifit is
aprime number the output should be the square root of the number up to 2 decimal point
precision, Ifitis not a prime number then print 0.00 to stdout.
Solution:
#include
#include
#include int main(int a, char *b[])
{
int number flag = 4;
number = atoi(b[1]);
for(i:
{
if(number%i
{
flag = 0;
; i=0;i-)
printf("%ad", bli);
return 0;
]
TCS Ninja Coding question 8:
Write ac 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, N41 which is a
positive integer, will contain the lower bound of the range. The second command line
htps:ww faceprep.infes-ina-coding-questions! anastro
‘TOS Ninja coding questions and answers -
parameter N2, which is also a positive integer will contain the upper bound of the range. The
and “24” here N1= 7 and N2=24, expected output as 83.
Solutio
#include
int main(int argc, char ‘argv{])
{
int N4, N2, j,i, count, sum = 0;
N1 =atoi(argv[1));
N2 =atoilargv[2]);
count;
break;
3
}
iffcount==0)
sum=sum +i;
}
printf("9%d""sum);
return 0;
]
TCS Ninja Coding question 9:
htps:ww faceprep.infes-ina-coding-questions!
onaar7eo%8 ‘TOS Ninja coding questions and answers -
Write aC program to check whether the given number is a perfect square or not using
#include
#finclude
int main(int a, char *b{])
{
intn,
n= atoi(b[1));
for(i =0;i<=n;i++)
{
if(n==i* i)
{
printf("YES");
return0;
}
}
printf("NO");
return 0;
}
TCS Ninja Coding question 10:
Write aC program to check whether the given number is Palindrome or not using command
line arguments.
Solution:
#include
#include
int main(int a,int *b{))
i
htps:ww faceprep.infes-ina-coding-questions! ronar7eo%8 ‘TOS Ninja coding questions and answers -
int number, rem, sum = 0;
{
rem =number%10;
sum = sum * 10 + rem;
number = number/10;
3
if(copy
printf(‘Palindrome”);
sum)
else
printf("Not Palindrome’);
return 0;
}
TCS Ninja Coding question 11:
Write aC program to convert the vowels to an uppercase in a given string using command
line arguments.
Example: ifthe input is tata, then the expected output is tAtA.
Solution:
#include
int main(int argc, char ‘argv{))
{
char *str = argv(1];
htps:ww faceprep.infes-ina-coding-questions! nnaar7eo%8 ‘TOS Ninja coding questions and answers -
str[i] = str[i] - 32;
return 0;
}
TCS Ninja Coding question 12:
Write aC program to find the hypotenuse of a triangle using command line arguments.
Solution:
#include int main(int a, char*b[})
{
float hyp;
int opp=atoi(b[1));
int adj=atoi(b[2));
hyp=sqrt((opp*opp)+(adj*adj));
printf(‘%0.2f"hyp);
return 0;
3
TCS Ninja Coding question 13:
Write aC program to find whether the given number is an Armstrong number or not using
command line arguments.
‘An Armstrong number of three digits is an integer such that the sum of the cubes of its digits
is equal to the number itself. For example, 371 is an Armstrong number since 3°°3 + 7"*3 +
173 =371,
Solution:
htps:ww faceprep.infes-ina-coding-questions! ranear7eo%8 ‘TOS Ninja coding questions and answers -
#include
intn;
n= atoi(b[1));
n=temp;
while(n!=0)
{
int rem=n%10;
sum=sum+pow(rem,cnt);
n=n/10;
}
if(temp==sum)
{
printf("yes");
}
else
{
printf(‘no");
}
return 0;
]
TCS Ninja Coding question 14:
htps:ww faceprep.infes-ina-coding-questions! sanaar7eo%8 ‘TOS Ninja coding questions and answers -
Write a program to generate Fibonacci Series.
\clude
#include
int main(int a, char *b[])
|, t1 = 0, t2 = 1, nextTerm;
bla);
ji<= nj; ++i)
printf("%d * t1);
nextTerm = t1 + t2;
t= t2;
t2=nextTerm;
}
return 0;
}
htps:ww faceprep.infes-ina-coding-questions!
sai