Coding Statements Useful For Tcs Ninja - With Solution
Coding Statements Useful For Tcs Ninja - With Solution
Coding Statements Useful For Tcs Ninja - With Solution
Input Format
Given a single line input separated by space.N1 denotes the numbers of row N2 denotes
the number of columns.
Output Format
Constraints
Integers only.
1≤N1≤100
1≤N2≤100
Sample Testcases
5 5 1 1 1 1 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 1 1 1 1
Solution:
#include<stdio.h>
int main()
int row,col;
int i,j;
scanf("%d%d",&row,&col);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
printf("1");
else
printf("0");
printf("\n");
Input Format
Number to searched(X)
In Sample Input,
So, output is 79
Output Format
Sample Testcases:
10 7 9
1 2 2 2 2 3 4 8 8 8
Solution:
#include<stdio.h>
int main()
int size,i,ele;
int first=-1,last=-1;
scanf("%d",&size);
www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139
int arr[size];
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
scanf("%d",&ele);
for(i=0;i<size;i++)
first = i;
last =i;
last =i;
if(first==-1)
printf("no occurance");
printf("%d %d",first,last);
return 0;
Q3) Print the given matrix in spiral form as displayed in sample output. Get the row and
column value from user and get elements based on row and column value.
Input Format
Get the value of N to create N*M matrix, followed by the elements of the matrix in
respective row and column order.
Output Format
Constraints
N,M> 0
N,M≤ 100.
Sample Testcases:
3 3 1 2 3 6 9 8 7 4 5
1 2 3
4 5 6
7 8 9
www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139
Testcase 2 Input Testcase 2 Output
5 5 12 34 56 78 89 98 55 22 33 88 76 45 12 90 11 43 56 21 67 44 43
66 87 22 33
12 34 56 78 89
43 56 21 67 98
11 22 33 44 55
90 87 66 43 22
12 45 76 88 33
Solution:
#include<stdio.h>
/*
123
456
789
*/
int main()
{
int arr[][3] = {{1,2,3},{4,5,6},{7,8,9}};
int row=3,col=3;
int i=0,j=0;
int temp_r=0,temp_c=0;
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 termin the odd 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 thterm,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.
Solution:
#include<stdio.h>
int oddVals(int n)
www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139
{
int i;
int val = 1;
for(i=2;i<=n;i++)
val = val*2;
return val;
int evenVals(int n)
int i;
int val = 1;
for(i=2;i<=n;i++)
val = val*3;
return val;
int main()
int n;
scanf("%d",&n);
if(n%2==0)
printf("%d",evenVals(n/2));
else
printf("%d",oddVals(n/2+1));
Input Format
Output Format
Constraints
X≥0
Y≥0
Sample Testcases:
www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139
Testcase 1 Input Testcase 1 Output
77 99 11
-1 0 Wrong Input
Solution:
#include<stdio.h>
int main()
int a,b,i,min,gcd;
scanf("%d%d",&a,&b);
if(a<0 || b<0)
printf("wrong input");
return -1;
min = a<b?a:b;
for(i=min-1;i>=2;i--)
gcd =i;
break;
if(i==1)
gcd = min;
printf("GCD: %d\n",gcd);
return 0;
2*2
3*3*3
4*4*4*4
4*4*4*4
3*3*3
2*2
Input Format
Output Format
Constraints
Sample Testcases:
Solution :
#include<stdio.h>
int main()
{
int i,j,n;
printf("enter value of n: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=2*i-1;j++)
{
if(j%2!=0)
{
printf("%d",i);
}
else
{
printf("*");
}
}
printf("\n");
}
for(i=n;i>=1;i--)
www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139
{
for(j=1;j<=2*i-1;j++)
{
if(j%2!=0)
{
printf("%d",i);
}
else
{
printf("*");
}
}
printf("\n");
}
return 0; }
Input Format
Output Format
Sample Testcases:
1 10 NO ELEMENT FOUND
Solution:
www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139
#include<stdio.h>
#include<math.h>
int arr[100];
int temp=no;
int res=0,deg=0,i;
while(no>0)
arr[deg] = no%10;
no = no/10;
deg++;
for(i=0;i<deg;i++)
res += (int)pow(arr[i],deg);
if(res==temp)
return 1;
else
return 0;
int main()
int start,end,i;
scanf("%d%d",&start,&end);
if(start<0||end<0)
printf("Wrong input");
return -1;
for(i=start;i<=end;i++)
if(isArmStrong(i)==1)
printf("%d\t",i);
return 0;
Q.8.Find Anagrams in array of Strings. You are given an array of strings and you have to
print all the anagrams within the array.What is anagram — For those who don't know,
two words are anagrams if they contain the same characters.
Input Format
Output Format
Sample Testcases:
cat,act
Solution:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Word {
};
struct DupArray {
};
dupArray->size = size;
int i;
dupArray->array[i].index = i;
strcpy(dupArray->array[i].str, str[i]);
return dupArray;
// individual words.
int i;
qsort(dupArray->array[i].str,
int main()
printAnagramsTogether(wordArr, size);
return 0;
Q.9) Given a time in 12-hour AM/PM format, convert it to railway (24-hour) time.
Input Format
Output Format
Constraints
08:00:000PM 20:00:00
Solution:
#include<stdio.h>
#include<string.h>
//00:00:00 PM
int main()
char time[13]={0};
char ampm[3]={0};
int hh,mm,ss;
scanf("%s",time);
scanf("%s",ampm);
hh = atoi(strtok(time,":"));
mm = atoi(strtok(NULL,":"));
ss = atoi(strtok(NULL,":"));
if((strcmp(ampm,"PM")==0 || strcmp(ampm,"pm")==0)&&
(hh!=0)&&(hh!=12))
hh = hh+12;
}
www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139
if((strcmp(ampm,"AM") == 0) || (strcmp(ampm,"am") == 0) && (hh == 12))
hh = 0;
else
return 0;
Q.10)Count the number of pairs in integer array whose sum equals given sum (all
elements are unique).
Example:
Sum =10,
Input Format
Output Format
N <= 50
10 3
0 2 5 7 4 6 10 20 -10
Solution:
#include<stdio.h>
int main()
int size;
int i,j,sum;
scanf("%d",&size);
int arr[size];
printf("enter elements");
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
scanf("%d",&sum);
for(j=i+1;j<size;j++)
if(arr[i]+arr[j]==sum)
printf("(%d,%d)",arr[i],arr[j]);
return 0;
Read a minimum and maximum number and print the all prime numbers between the given
range.
Case1
Input (stdin)
20
120
Expected Output (stdout)
23
29
31
37
41
43
47
53
Input (stdin)
1
20
Expected Output (stdout)
2
3
5
7
11
13
17
19
Your Output (stdout)
No response
Response
Case3
1
10
Expected Output (stdout)
2
3
5
7
Your Output (stdout)
No response
Response
Solution:
#include<stdio.h>
#include<math.h>
int isPrime(int n)
{
int i;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
return 0;
}
}
return 1;
}
int main()
{
int start,end;
int i;
printf("enter range: ");
scanf("%d%d",&start,&end);
for(i=start;i<=end;i++)
www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139
{
if(isPrime(i))
{
printf("%d ",i);
}
}
Input (stdin)
5
Expected Output (stdout)
120
Your Output (stdout)
No response
Response
Case2
Input (stdin)
3
Expected Output (stdout)
6
Your Output (stdout)
No response
Response
Case3
www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139
Input (stdin)
12
Expected Output (stdout)
479001600
Your Output (stdout)
No response
Response
Solution:
#include<stdio.h>
int main()
int n,i,fact=1;
scanf("%d",&n);
for(i=1;i<=n;i++)
fact = fact*i;
printf("factorial : %d \n",fact);
return 0;
Input (stdin)
Input (stdin)
76
Expected Output (stdout)
114
Your Output (stdout)
No response
Response
Case 3
Input (stdin)
21
Expected Output (stdout)
25
Your Output (stdout)
No response
Response
Solution:
#include<stdio.h>
int main()
{
www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139
int bits[10];
int i=0,j,dec,rem;
scanf("%d",&dec);
while(dec>0)
rem = dec%8;
bits[i]=rem;
dec = dec/8;
i++;
for(j=i-1;j>=0;j--)
printf("%d",bits[j]);
return 0;
Q.14.English teacher informed the highest mark scored in her test in the class. Your task is
to find the second highest mark.
Marks scored in the test is saved in an array and you have to find the second maximum in
the array.
Case 1
Input (stdin)
5
100
200
9
400
www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139
130
Expected Output (stdout)
No response
Response
Case 2
Input (stdin)
10
1
2
2
4
5
6
7
8
9
10
Expected Output (stdout)
No response
Response
Case 3
Input (stdin)
2
1
2
Expected Output (stdout)
www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139
second highest mark = 1
Your Output (stdout)
No response
Response
Solution:
#include<stdio.h>
int main()
int i,size=7,high=0,sec_high=-1;
for(i=0;i<size;i++)
if(arr[i]>high)
sec_high = high;
high = arr[i];
else if(arr[i]>sec_high)
{2
sec_high = arr[i];
return 0; }
Input (stdin)
10
1 2 3 4 5 6 7 8 9 10
4 10
Expected Output (stdout)
e1 index: 3
e2 index: 9
Your Output (stdout)
No response
Response
Case 2
Input (stdin)
10
11 21 31 42 33 16 17 18 10 5
18 44
Expected Output (stdout)
e1 index: 7
e2 index: -1
Your Output (stdout)
No response
Response
Case 3
Input (stdin)
10
www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139
9 8 7 6 5 4 3 2 1 11
12 4
Expected Output (stdout)
e1 index: -1
e2 index: 5
Your Output (stdout)
No response
Response
Solution:
#include<stdio.h>
int main()
int e1,e2,f1=-1,f2=-1,i,size=7;
scanf("%d%d",&e1,&e2);
for(i=0;i<size;i++)
if(arr[i]==e1)
f1 = i;
if(arr[i]==e2)
f2 = i;
}
www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139
}
if(f1==-1)
else
if(f2==-1)
else
return 0;
Case1
Input (stdin)
10.0.14.255
No response
Response
Case2
Input (stdin)
10.132.14.256
Invalid
No response
Response
Case3
Input (stdin)
123.1.3.0
Valid
No response
Response
Solution:
#include<stdio.h>
#include<string.h>
//255.255.255.255
int main()
{
www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139
char arr[16];
scanf("%s",arr);
printf(arr);
if(atoi(section)<0 || atoi(section)>255)
printf("invalid ip");
return -1;
int i;
for(i=0;i<3;i++)
section = strtok(NULL,".");
if(atoi(section)<0 || atoi(section)>255)
printf("invalid ip");
return -1;
printf("valid ip");
return 0;
Read a string from the user and count total number of words in that.
Case1
Input (stdin)
Hello world
Expected Output (stdout)
2
Your Output (stdout)
No response
Response
Case2
Input (stdin)
Interactive programming environment
Expected Output (stdout)
3
Your Output (stdout)
No response
Response
Case3
Input (stdin)
Focus academy for career enhancement
Expected Output (stdout)
5
Your Output (stdout)
No response
Response
#include<stdio.h>
#include<string.h>
int main()
char arr[100]={0};
int i;
int words=1;
fgets(arr,sizeof(arr),stdin);
for(i=0;i<strlen(arr);i++)
words++;
printf("words: %d",words);
Case1
Input (stdin)
a
Expected Output (stdout)
A
Your Output (stdout)
No response
Response
Case2
Input (stdin)
A
Expected Output (stdout)
a
Your Output (stdout)
No response
Response
Case3
Input (stdin)
b
Expected Output (stdout)
B
Your Output (stdout)
No response
Response
#include<stdio.h>
int main()
char a;
printf("enter character:");
scanf("%c",&a);
printf("%c",(97+(a%65)));
printf("%c",(65+(a%97)));
Q.19) Sachin has come up with a program to get 5 numbers from user and check whether
each number is prime or not. Unfortunately there is a bug in his code and he is stuck
fixing it. Help him through. When he gives the following 5 numbers, his code is checking
only the first two numbers (10,17) for prime and does not check for others. 10 17 44 49 88
CASE 1)
Input (stdin)
1
2
3
1 is prime
2 is prime
3 is prime
4 is not prime
5 is prime
Your Output (stdout)
No response
Response
CASE 2
Input (stdin)
10
17
44
49
88
Expected Output (stdout)
10 is not prime
17 is prime
44 is not prime
49 is not prime
88 is not prime
Your Output (stdout)
No response
Response
CASE 3
Input (stdin)
No response
Response
Solution:
#include<stdio.h>
#include<math.h>
int isPrime(int n)
int i;
for(i=2;i<=n/2;i++)
if(n%i==0)
return 0;
return 1;
int start,end;
int i;
scanf("%d%d",&start,&end);
for(i=start;i<=end;i++)
if(isPrime(i))
printf("\n%d is prime",i);
else
Q.20)
The Science teacher informed the first and second highest mark scored in the class. Your
task is to find the third-highest mark.
Marks scored in the test is saved in an array and you have to find the third maximum
number in an array.
CASE 1
Input (stdin)
32
Your Output (stdout)
No response
Response
CASE 2
Input (stdin)
6
90 67 18 45 31 67
Expected Output (stdout)
67
Your Output (stdout)
No response
Response
CASE 3
Input (stdin)
7
1 4 6 7 9 3 17
Expected Output (stdout)
7
Your Output (stdout)
No response
Solution:
#include <stdio.h>
if (arr_size < 3)
return;
third = second;
second = first;
first = arr[i];
third = second;
second = arr[i];
third = arr[i];
int main()
int n = sizeof(arr)/sizeof(arr[0]);
print3largest(arr, n);
return 0;
Q.21. Print the stars in the half diamond shape based on the given input.
The given input will be taken as N. If the input is 4, then the output should be * ** *** ****
*** ** *
CASE 1
Input (stdin)
7
Expected Output (stdout)
*
**
***
****
*****
******
*******
******
*****
No response
Response
CASE 2
Input (stdin)
5
Expected Output (stdout)
*
**
***
****
*****
****
***
**
*
Your Output (stdout)
No response
Response
CASE3
Input (stdin)
4
Expected Output (stdout)
No response
Response
Solution:
#include<stdio.h>
int main()
int n;
int i,j;
printf("enter n");
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<=i;j++)
printf("*");
printf("\n");
for(i=0;i<n-1;i++)
{
www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139
for(j=i;j<n-1;j++)
printf("*");
printf("\n");