Вы находитесь на странице: 1из 49

# www.talentbattle.

## in TCS Ninja Useful Coding Statements +91-8459943139

Q1) Write a program to print the pattern such that the elements of first row, first
column,last row and last column should be one and the remaining elements should be
zero.

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

## Testcase 1 Input Testcase 1 Output

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;

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

printf("enter number of rows and columns:");

scanf("%d%d",&row,&col);

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

for(j=0;j<col;j++)

printf("1");

else

printf("0");

printf("\n");

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

Q2) Given a sorted array with many duplicate elements, the problem is to find indexes of
first and last occurrences of an element X in the given array.Start the array index from 0.
If element X is not present in the array,print "NO OCCURENCES".

Input Format

## N array elements in second line

Number to searched(X)

In Sample Input,

So, output is 79

Output Format

## The output consists of indexes of first and last occurrence of an element X.

Sample Testcases:

## Testcase 1 Input Testcase 1 Output

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;

## printf("enter size of an array");

scanf("%d",&size);
www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139
int arr[size];

## printf("enter array elements");

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

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

## printf("enter element to searh for first and last orccurance");

scanf("%d",&ele);

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

first = i;

last =i;

## else if(arr[i]==ele && first!=-1)

last =i;

if(first==-1)

printf("no occurance");

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

else

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.

## The matrix can be either square or rectangle.

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

## Note : There is a space at the end of the output.

Constraints

N,M> 0

N,M≤ 100.

Sample Testcases:

## Testcase 1 Input Testcase 1 Output

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;

## while(temp_r<row && temp_c<col)

{
for(i=0;i<col;i++)
{
printf("%d",arr[temp_r][i]);
}
temp_r++;
for(i=temp_r;i<row;i++)
{
printf("%d",arr[i][col-1]);
}
col--;
www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139
if(temp_r<row)
{
for(i=col-1;i>=temp_c;i--)
{
printf("%d",arr[row-1][i]);
}
row--;
}
if(temp_c<col)
{
for(i=row-1;i>=temp_r;i--)
{
printf("%d",arr[i][temp_c]);
}
temp_c++;
}
}
return 0;
}

## Q.4) 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 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.

## You can assume that N will not exceed 30.

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;

## printf("enter value of n to find nth number in series:");

scanf("%d",&n);

if(n%2==0)

printf("%d",evenVals(n/2));

else

printf("%d",oddVals(n/2+1));

Input Format

Output Format

## Display the output as shown in the sample output.

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

## Testcase 2 Input Testcase 2 Output

-1 0 Wrong Input

Solution:

#include<stdio.h>

int main()

int a,b,i,min,gcd;

## printf("enter two numbers: ");

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;

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

}

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

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

N>0

Sample Testcases:

## Testcase 1 Input Testcase 1 Output

4 1
2*2
3*3*3
4*4*4*4
4*4*4*4
3*3*3
2*2
1
Testcase 2 Input Testcase 2 Output
-1 wrong Input

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

## Display the output as shown in the sample output.

Sample Testcases:

## Testcase 1 Input Testcase 1 Output

1 10 NO ELEMENT FOUND

## -1 100 wrong Input

Solution:
www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139
#include<stdio.h>

#include<math.h>

## int isArmStrong(int no)

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;

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

printf("enter start and end of range");

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

## Anagrams separated by comma.

Sample Testcases:

## Testcase 1 Input Testcase 1 Output

www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139
7 bat code cat tab cab crazy act bat, tab

cat,act

Solution:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct Word {

## int index; // index of the word in the original array

};

struct DupArray {

};

## struct DupArray* dupArray = (struct DupArray*)malloc(sizeof(struct DupArray));

dupArray->size = size;

int i;

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

dupArray->array[i].index = i;

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

dupArray->array[i].str = (char*)malloc(strlen(str[i]) + 1);

strcpy(dupArray->array[i].str, str[i]);

return dupArray;

## // Step 2: Iterate through all words in dupArray and sort

// individual words.

int i;

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

qsort(dupArray->array[i].str,

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

strlen(dupArray->array[i].str), sizeof(char), compChar);

int main()

## int size = sizeof(wordArr) / sizeof(wordArr[0]);

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

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

Sample Testcases:

## Testcase 1 Input Testcase 1 Output

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;

## printf("enter time in 12hr format(hh:mm:ssAM/PM)= ");

scanf("%s",time);

scanf("%s",ampm);

hh = atoi(strtok(time,":"));

mm = atoi(strtok(NULL,":"));

ss = atoi(strtok(NULL,":"));

## if(hh<=12 && mm<=59 && ss<=59)

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

## printf("invalid date input");

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

## Output the number of pairs.

www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139
Constraints

N <= 50

## Test case 1 Input Test case 1 Output

10 3

0 2 5 7 4 6 10 20 -10

Solution:

#include<stdio.h>

int main()

int size;

int i,j,sum;

## printf("enter size of an array:");

scanf("%d",&size);

int arr[size];

printf("enter elements");

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

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

## printf("enter sum: ");

scanf("%d",&sum);

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

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

for(j=i+1;j<size;j++)

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

printf("(%d,%d)",arr[i],arr[j]);

return 0;

## Q.11)Program to print Prime numbers within range.

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

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

59
61
67
71
73
79
83
89
97
101
103
107
109
113
No response
Response
Case2

Input (stdin)

1
20
Expected Output (stdout)

2
3
5
7
11
13
17
19

No response
Response
Case3

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

Input (stdin)

1
10
Expected Output (stdout)

2
3
5
7

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

## Q.12Write a program to find the Factorial of the given number

Factorial of a number
Case1

Input (stdin)

5
Expected Output (stdout)

120

No response
Response
Case2

Input (stdin)

3
Expected Output (stdout)

6

No response
Response
Case3
www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139
Input (stdin)
12
Expected Output (stdout)

479001600
No response
Response

Solution:

#include<stdio.h>

int main()

int n,i,fact=1;

## printf("enter number: ");

scanf("%d",&n);

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

fact = fact*i;

printf("factorial : %d \n",fact);

return 0;

CASE1

Input (stdin)

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

3
Expected Output (stdout)
3
No response
Response
CASE 2

Input (stdin)

76
Expected Output (stdout)

114

No response
Response
Case 3

Input (stdin)
21
Expected Output (stdout)
25
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;

## printf("enter decimal number: ");

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)

## second highest mark = 200

No response
Response
Case 2

Input (stdin)

10
1
2
2
4
5
6
7
8
9
10
Expected Output (stdout)

## second highest mark = 9

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

No response
Response

Solution:

#include<stdio.h>

int main()

int i,size=7,high=0,sec_high=-1;

## int arr[] = {90,80,1,2,55,70,100};

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

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

Q.15.Get the two elements e1 and e2. If e1 & e2 found, then print their index else print -
1.

## 1) Size of an Array: 10 2) Input: 1 2 3 4 5 6 7 8 9 10 e1 = 4, e2 = 6 3) e1 index: 3 e2 index: 5

Case 1

Input (stdin)

10
1 2 3 4 5 6 7 8 9 10
4 10
Expected Output (stdout)

e1 index: 3
e2 index: 9

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

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

No response
Response

Solution:

#include<stdio.h>

int main()

int e1,e2,f1=-1,f2=-1,i,size=7;

## printf("enter two numbers to be searched");

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

Valid

No response

Response
Case2
Input (stdin)

10.132.14.256

Invalid

No response

Response
Case3
Input (stdin)

123.1.3.0

## Expected Output (stdout)

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

## char* section = strtok(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;

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

Q.17)Program to print number of words

Read a string from the user and count total number of words in that.
Case1
Input (stdin)
Hello world
Expected Output (stdout)
2
No response
Response

Case2
Input (stdin)
Interactive programming environment
Expected Output (stdout)
3
No response
Response

Case3
Input (stdin)
Expected Output (stdout)
5
No response
Response

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

Solution:

#include<stdio.h>

#include<string.h>

int main()

char arr[100]={0};

int i;

int words=1;

## printf("enter text: ");

fgets(arr,sizeof(arr),stdin);

for(i=0;i<strlen(arr);i++)

## if(arr[i]==' ' && arr[i+1]!=' ')

words++;

printf("words: %d",words);

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

Q.18) Change the case of the given alphabet and print

Case1

Input (stdin)

a
Expected Output (stdout)

A

No response
Response
Case2

Input (stdin)

A
Expected Output (stdout)

a

No response
Response
Case3

Input (stdin)
b
Expected Output (stdout)
B
No response
Response

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

Solution:

#include<stdio.h>

int main()

char a;

printf("enter character:");

scanf("%c",&a);

## if(a>=64 && a<=(65+26))

printf("%c",(97+(a%65)));

## else if(a>=97 && a<=(97+26))

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

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

4
5
Expected Output (stdout)

1 is prime
2 is prime
3 is prime
4 is not prime
5 is prime

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

No response
Response

CASE 3

Input (stdin)

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

187
357
147
157
557
Expected Output (stdout)

## 187 is not prime

357 is not prime
147 is not prime
157 is prime
557 is prime

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

if(isPrime(i))

printf("\n%d is prime",i);

else

## printf("\n%d is not prime",i);

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)

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

34 7 12 98 32
Expected Output (stdout)

32

No response
Response

CASE 2

Input (stdin)

6
90 67 18 45 31 67
Expected Output (stdout)

67

No response
Response

CASE 3

Input (stdin)

7
1 4 6 7 9 3 17
Expected Output (stdout)

7

No response

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

Response

Solution:

#include <stdio.h>

## int i, first, second, third;

if (arr_size < 3)

return;

third = second;

second = first;

first = arr[i];

third = second;

second = arr[i];

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

else if (arr[i] > third)

third = arr[i];

int main()

## int arr[] = {12, 13, 1, 10, 34, 1};

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)

*
**
***
****
*****
******
*******
******
*****

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

****
***
**
*

No response
Response

CASE 2

Input (stdin)

5
Expected Output (stdout)

*
**
***
****
*****
****
***
**
*

No response
Response

CASE3

Input (stdin)

4
Expected Output (stdout)

## www.talentbattle.in TCS Ninja Useful Coding Statements +91-8459943139

*
**
***
****
***
**
*

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

## Нижнее меню

### Получите наши бесплатные приложения

Авторское право © 2021 Scribd Inc.