## Coding Questions asked in TCS Ninja

We strongly recommend to visit the following page here to learn Command Line Programming, before trying to solve questions below.

1. Number of Questions – 1
2. Time – 20 mins

Get all the questions asked Today in TCS here –

TCS Ninja Live Questions Dashboard

Basic Study Material (Expect about 60% from here)

No online class in above only study material. Only Previous Papers, if you want to Register for Online Class Register here.

Pro Study Material (2x more questions 4x more chance of repetition)

No online class in above only study material. Only Previous Papers, if you want to Register for Online Class Register here.

### Free Material

TCS Ninja Coding Questions and Answers are graded by the TCS Server and not by the human, when you compile the program, the system checks for the following to give you marks –

 a. Correct output on various test cases b. Partial output at most obvious cases c. logical error present in the codes d. is the program compilation done.

There are a lot of free sections given above if you want to study more, you can buy TCS Coding Questions Paid materials which are basically command line questions.

Find the nth term of the series.

1,1,2,3,4,9,8,27,16,81,32,243,….

#include
#include
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);
}

Question – 2

Link to this Question

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.

You can assume that N will not exceed 30.

#include
#include

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 = pow(2, term_in_series – 1);
printf(“%d “, res);
}
else
{
int a = 1;
int r = 3;
int term_in_series = n/2;
int res = pow(3, term_in_series – 1);
printf(“%d “, res);
}

return 0;
}

Extra Questions without Solutions and Options that students couldn’t remember, please add solutions in the comment section below –

1. Given a series whose even term creates a separate geometric series and odd term creates another geometric series . Prog in any language to find the nth term. Where u may consider that n not greater dan 30.
2. 1,1,2,2,4,4,8,8,16,16 also this code

Solutions to one of the problem discussed above –

#include
#include
#includelong long int power(long long int a,long long int b ){

long long int i,ans=1;
for(i=0;i<b;i++)
{
ans=ans*a;
}
return ans;
}

// long long int three(long long int n){
// return pow(3,n);
// }

// long long int two(long long int n){

// return pow(2,n);
// }

int main()
{
clrscr();
long long int i,nth,a[1000];
scanf(“%lld”,&nth);
for(i=0;i<nth;i++)
{
if(i%2==0)
{
a[i]=power(2,i/2);
}
if(i%2==1)
{

a[i]=power(3,(i/2)+1);
}

}

printf(“%lld “,a[nth-1]);

return 0;
}

Question 3

Link to this Question –

Consider the below series :

0,0,2,1,4,2,6,3,8,4,10,5,12,6,14,7,16,8

This series is a mixture of 2 series all the odd terms in this series form even numbers in ascending order and every even 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 term is (8/2)=4. Only the value 4 should be printed to STDOUT.

You can assume that the n will not exceed 20,000.

Code:

#include
#include

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;

int res = term_in_series – 1;
printf(“%d “, res);
}

return 0;
}

Solution not Available in other languages as of now- Please add in comments

Question 4

0,0,2,1,4,2,6,3,8,4,10,5,12,6….

Link to this Question

#include
int main()
{
int i=3,n;
printf(“enter nth term”);
scanf(“%d”,&n);
int a[n+1];
a[1]=0;
a[2]=0;
while(i<=n)
{
if(i%2==0)
a[i]=a[i-2]+1;
else
a[i]=a[i-2]+2;
i++;
}
printf(“%d”,a[n]);
}

Question 5

Link to this Questions

1. The program will recieve 3 English words inputs from STDIN

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

Write Code for this

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char *str1=malloc(sizeof(char)*256);
char *str2=malloc(sizeof(char)*256);
char *str3=malloc(sizeof(char)*256);
printf(“ENter 3 words : “);
scanf(“%s%s%s”,str1,str2,str3);

int p1=strlen(str1);
int p2=strlen(str2);
int p3=strlen(str3);
for(int i=0;i<p1;i++)
{
if(str1[i]==’a’||str1[i]==’e’||str1[i]==’i’||str1[i]==’o’||str1[i]==’u’)
{
str1[i]=’\$’;
}
}
for(int i=0;i<p2;i++)
{
if(str2[i]!=’a’ && str2[i]!=’e’ && str2[i]!=’i’ && str2[i]!=’o’ && str2[i]!=’u’)
{
str2[i]=’#’;
}
}
for(int i=0;i<p3;i++)
{
str3[i]=str3[i]-32;
}

printf(“\n%s”,str1);
printf(“\n%s”,str2);
printf(“\n%s”,str3);

return 0;

}

#include<stdio.h>

void main()

{

int n,i,j;

printf(“enter no of words \n”);

scanf(“%d”,&n);

char a[n][20];

for(i=0;i<n;i++)

{

scanf(“%s”,a[i]);

}

for(i=0;i<n;i++)

{

if(i==0)

{

for(j=0;a[i][j]!=’\0′;j++)

{

if(a[i][j]==’a’||a[i][j]==’e’||a[i][j]==’i’||a[i][j]==’o’||a[i][j]==’u’)

{

a[i][j]=’\$’;

}

}

}

Question 6

look at the series below: 1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,…..

This series is formed as below: 1.term(1)=1

2.term(2)=2

3.term(N)=term(N-1)+term(N-2)for N>2

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 and messages should be written to STDOUT.

For example if N =15,the value of 15thn term is 987 which is the sum of 13th and 14th terms . You can assume that the value of n will not exceed 30

Ans:

#include <stdio.h>

int main() {

//code int n;

scanf(“%d”, &n);

if(n == 1)

{

printf(“1”);

}

else if(n ==2)

{

printf(“2”);

}

else

{

int t1=1, t2=2, nth_term; for(int i = 3; i <=n; i++)

{

nth_term = t1 + t2; t1 = t2;

t2 = nth_term;

}

printf(“%d”, nth_term);

}

return 0;

}

Question 7

Link to this code is here

look at the series below: 1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,…..

This series is formed as below: 1.term(1)=1

2.term(2)=2

3.term(N)=term(N-1)+term(N-2)for N>2

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 and messages should be written to STDOUT.

For example if N =15,the value of 15thn term is 987 which is the sum of 13th and 14th terms . You can assume that the value of n will not exceed 30

Ans:

#include <stdio.h>

int main() {

//code int n;

scanf(“%d”, &n);

if(n == 1)

{

printf(“1”);

}

else if(n ==2)

{

printf(“2”);

}

else

{

int t1=1, t2=2, nth_term; for(int i = 3; i <=n; i++)

{

nth_term = t1 + t2; t1 = t2;

t2 = nth_term;

}

printf(“%d”, nth_term);

}

return 0;

}

Question 8

aaaabbBccdee change to a4b2B1c2d1e2

Write code for this in the comment section below –

int main()

{

char a[100];

int i,count=0;

scanf(“%s”,a);

for(i=0;a[i]!=’\0′;i++)

{

count=1;

while(a[i]==a[i+1])

{

i++;

count++;

}

printf(“%c%d”,a[i],count);

}

return 0;

}

Programming auestion :replacing multiple occurances of characters by single character and printing in reverse order  if input:aaaaEEeeecccCCCGEExffe then output:efxEGCceEa

Extra Questions

Question 0

Find the nth term of the series.

1,1,2,3,4,9,8,27,16,81,32,243,….

`#include<stdio.h>#include<math.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);}`

Question 1

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 fail 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 in a positive integer that should be read from mm. The Nth term that is calculated by the program should be written to STDOUT Otherthan the value of Nth term , no other characters / string 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

Solution –

`#includevoid fibo(int);void prime(int);main(){int n,e;scanf("%d",&n);e=n/2;if(n%2==0)prime(e);elsefibo(e+1);}void prime(int n){int i,j,no,flag=0,count=0;  for(i=1;i<=100;i++)   {flag=0;       for(j=2;j<=i/2;j++)       {if(i%j==0)flag=0;elseflag=1;         }   if(flag==1)    count++;    if(count==n)        {            printf("%d\n",i);            break;        }    }    }void fibo(int n){  int n0=0,n1=1,n2,i;for(i=3;i<=n;i++){    n2=n0+n1;    n0=n1;    n1=n2;}printf("%d",n2);}Question 2`

Here is the program for gcd of two numbers using command line arguments check other sections for TCS on our website.

`#includeint main(int a,char *b[]) { int num1,num2,min,i,gcd=1;if(a!=3){ printf("Enter two argument\n"); exit(1); } num1=atoi(b[1]);num2=atoi(b[2]); min=(no1<no2)?no1:no2;for(i=1;i<=min;i++) {if((num1%i)==0 && (num2%i)==0) gcd=i; } printf("GCD of two number %d",gcd); return 0; }`

Question 3

Binary to Decimal Conversion

```#include
#include
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;
}
}```

Question 4

Armstrong Number

`#include#include`

int main(int argc,char *argv[])
{
int Given_number= atoi(argv[1]);
int num;
for(num=1; num<=Given_number; num++)
{
int a=num;
int s=0;
int r=0;

while(a>0)
{
s=a%10;
r=r+(s*s*s);
a=a/10;
}
if(r==num)
printf(” %d isarmstrong no \n”, num);
}
}

Question 5

Write a C program to find the area of a circle with radius provided.
The value of radius positive integer passed to the program as the first command line parameter. Write the output to stdout formatted as a floating point number rounded to EXACTLY 2 decimal precision WITHOUT any other additional text.
Scientific format(such as 1.00E+5) should NOT be used while printing the output.
You may assume that the inputs will be such that the output will not exceed the largest possible real number that can be stored in a float type variable.

It is highly advisable to go through Command Line Arguments Post before even looking at the code. Please study this for TCS and come back to this post later.

```#include
#include
int main(int argc, char * argv[])
{
if(argc==1)
{
printf("No arguments");
return 0;
}
else
{
float pi=3.14;
float area;
printf("%.2f",area);
return 0;
}
}```

Write a C program to find the area of a circle with radius provided.
The value of radius positive integer passed to the program as the first command line parameter. Write the output to stdout formatted as a floating point number rounded to EXACTLY 2 decimal precision WITHOUT any other additional text.
Scientific format(such as 1.00E+5) should NOT be used while printing the output.

Question 6

Command Line Program to check if a year is Leap Year or Not

```#include
void main(int argc,char *argv[])
{
int n;
n=atoi(argv[1]);
if(n%4==0)
{
if(n%100==0)
{
if(n%400==0)

printf("Leap Year");
else
printf("Not Leap Year");
}
else
printf("Leap Year");
}
else
printf("Not Leap Year");
getch();

}```

Question 7

Fibonacci Series

```#include
#include

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

{

int n, first = 0, second = 1, next, c;

n = atoi(argv[1]);

printf("These are %d values in Fibonacci series are by PrepInsta:-\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;

}```

Question 8

Ques. Write a C program to find the area of a triangle given the base and the corresponding height. The values base and height are both positive integers passed to the program as the first and second command line parameters respectively. Write the output to stdout formatted as a floating point number rounded to EXACTLY 2 decimal precision WITHOUT any other additional text. Scientific format(such as 1.00E+5) should NOT be used while printing the output. You may assume that the inputs will be such that the output will not exceed the largest possible real number that can be stored in a float type variable.

`#include#include int main(int argc, char *argv[]){ if (argc < 3)// as number of arguments needed are 2 and 1 is default arg. {printf(" Please provide values for both base and height \n");return 0;} else{int base = atoi(argv[1]);int height = atoi(argv[2]);float area = 0.5*base*height;printf("%.2f",area);return 0;}}`

Question 9

Palindrome Number

```#include

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;
}```

Question 10

Decimal to Binary

`#include#includeint 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;
}
}

Question 11

Binary to Octal

`#include`

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

{

ong int n,r,c,b=1,s=0;

n=atoi(argv[1]);

c=n;

while(c!=0)

{

r=c%10;

s=s+r*b;

c=c/10;

b=b*2;

}

printf(“%lo”,s);

getch();

}

Question 11

Decimal to Octal

`#include`

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

{

int n,s=0,b=1,r;

n=atoi(argv[1]);

int c=n;

while(c>0)

{

r=c%8;

s=s+r*b;

c=c/8;

b=b*10;

}

printf(“%d”,s);

getch();

}

Question 12

String Palindrome

`#include`

#include

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);

}

Question 13

`#include`

#include

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;

}

}

Question 14

Square Root without using Math.h

`#include`

#include

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);

}

}

Question 15

Average of two numbers

#include

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

{

int sum = 0,i = 1,count = 0;

if(argc == 1)

{

printf(“Enter the number \n”);

exit(1);

}

count = argc – 1;

while (i <= count )

{

sum += atoi (argv[i]) ;

i++;

}

printf(“Avg of the numbers.%d\n”, sum/count);

}

Question 16

Greatest of two Numbers

`#include`

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;

}

Question 17

Write a Program to print whether the given alphabet is vowel or consonant Solution:

`#include`

int main()

{

// Get the character char ch;

scanf(“%c”, &ch);

if(ch >= ‘A’ && ch <= ‘Z’)

{

ch = ‘a’ + (ch – ‘A’);

}

if(ch == ‘a’ || ch == ‘e’ || ch == ‘i’ || ch == ‘o’ || ch == ‘u’)

{

printf(“Vowel”);

}

else

{

printf(“Consonant”);

}
return 0;
}

Question 18

Write a program to Check whether a given number is a prime number or not Solution:

`#include`

#include

int main()

{

int n, i, flag = 0; //

printf(“Enter a positive integer: “);

scanf(“%d”,&n);

for(i=2; i<=sqrt(n); ++i) {

// condition for nonprime number

if(n%i==0)

{

flag=1;

break;

}

}

if (flag==0)

printf(“%d is a prime number.”,n);

else printf(“%d is not a prime number.”,n);

return 0;

}

Question 19

Write a program to change the case of the given alphabet and print. Solution:

`#include int main() {`

char c;

scanf(“%c”, &c);

// Upper to lower case

if(‘A’ <= c && c <= ‘Z’)

{

printf(“%c”, ‘a’ + (c – ‘A’));

}

// Lower to upper case if(‘a’ <= c && c <= ‘z’)

{ printf(“%c”, ‘A’ + (c – ‘a’));

}

return 0;

}

Question 20

Given an array and a number (say s), find whether any two elements in the array whose sum is “s”. Solution:

`#include #include`

void check_sum_and_diplay(int arr[], int size, int sum);

int main()

{

// Get the size of an array

int size;

scanf(“%d”, & size);

// Get the array elements

int arr[50], i;

for (i = 0; i < size; i++) {

scanf(“%d”, & arr[i]);

// Get the sum value (to check with an array elements)

int sum;
scanf(“%d”, & sum);

// Function call to check the sum of any two elements in an array equal to given sum

// and display the same

check_sum_and_diplay(arr, size, sum);

return 0;

}
}

void check_sum_and_diplay(int arr[], int size, int sum) {

int i, j;

for (i = 0; i < size – 1; i++)

{

{

if (sum == (arr[i] + arr[j]))

{

printf(“Perfect couple: %d %d”, arr[i], arr[j]);
exit(0);

}

}

printf(“No perfect couple found!”);
}

}

Question 21

Write a program to find the most occurring character in the string. Solution:

`#include`

#define MAX_SIZE 100

#define MAX_CHARS 26

int main()

{

int i;

//Get a sentence

char str[MAX_SIZE];

scanf(“%[^\n]s”, str);

// Init Freq stoting array

int freq[MAX_CHARS];

for(i=0; i<max_chars; i++)

{ freq[i]=0; }

//frequency of each character is counted

for(i=0; str[i]= !=’\0′;i++){

int isalphabet=0 , offset;

if(str[i]>= ‘a’ && str[i] <= ‘z’) {

isAlphabet = 1;

offset = str[i] – ‘a’;

}

else if(str[i] >= ‘A’ && str[i] <= ‘Z’)

{

isAlphabet = 1;

offset = str[i] – ‘A’;

}

if(isAlphabet == 1)

{

freq[offset] += 1;

}

}

// If two characters occurred the same number of time then

// print lowest ASCII value character. int max_index = 0; for(i=0; i<max_chars;i++)

{

if(freq[i] > freq[max_index])

{

max_index = i;

}

}

int max_repeated_char = ‘a’ + max_index;

printf(“%c”, max_repeated_char);

return 0;

}

Summary
Review Date
Reviewed Item
TCS Ninja Coding Questions
Author Rating
5