Download as pdf or txt
Download as pdf or txt
You are on page 1of 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

Print the output in the required format.

Note : There is a space and next line at the end of output.

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

if(i==0 || i==row-1 || j==0 || j==col-1)

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

First line of the input is number of elements(N)

N array elements in second line

Number to searched(X)

In Sample Input,

There are 10 elements in an array[1 2 2 2 2 3 4 8 8 8] and element to be searched(x) is 8.

So, output is 79

In the sample input,element 8 occurs in indexes 7 and 9.

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

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

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

Print the given matrix in spiral form(Refer Sample outputs)

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

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


{

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

Q5)Write a program to find the GCD of the given two numbers.

Input Format

Single line input separated by space contains two integer values X, Y.

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

if(a%i==0 && b%i==0)

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;

Q.6) Write a program to print below pattern.

2*2

3*3*3

4*4*4*4

4*4*4*4

3*3*3

2*2

Input Format

Input to get an integer N.

Output Format

Display the output as shown in the sample output.

Note: There is an extra line at the end of output.

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

Q.7)Write a program to print the Armstrong number between two intervals.

Input Format

Input contains two values separated by single space.

Output Format

Display the output as shown in the sample output.

Sample Testcases:

Testcase 1 Input Testcase 1 Output

1 10 NO ELEMENT FOUND

Testcase 2 Input Testcase 2 Output

100 200 153

Testcase 3 Input Testcase 3 Output

127 427 153 370 371 407

Testcase 4 Input Testcase 4 Output

-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

No.of strings followed by strings(space separated).

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 {

char* str; // to store word itself

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

};

struct DupArray {

struct Word* array; // Array of words

int size; // Size of array

};

struct DupArray* createDupArray(char* str[], int size)

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

dupArray->size = size;

dupArray->array = (struct Word*)malloc(dupArray->size * sizeof(struct Word));

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;

int compChar(const void* a, const void* b)

return *(char*)a - *(char*)b;

int compStr(const void* a, const void* b)

struct Word* a1 = (struct Word*)a;

struct Word* b1 = (struct Word*)b;

return strcmp(a1->str, b1->str);

void printAnagramsTogether(char* wordArr[], int size)

// Step 1: Create a copy of all words present in given wordArr.

// The copy will also have orignal indexes of words

struct DupArray* dupArray = createDupArray(wordArr, size);

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

// Step 3: Now sort the array of words in dupArray

qsort(dupArray->array, size, sizeof(dupArray->array[0]), compStr);

// Step 4: Now all words in dupArray are together, but these

// words are changed. Use the index member of word struct to

// get the corresponding original word

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

printf("%s ", wordArr[dupArray->array[i].index]);

// Driver program to test above functions

int main()

char* wordArr[] = { "cat", "dog", "tac", "god", "act" };

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

Time in 12 hour format as shown in sample input.

Output Format

Display time in 24 hours format as shown in sample output.

Constraints

Get the input as string

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;

printf("Time in 24 Hr format= %d:%d:%d",hh,mm,ss);

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,

No.of array elements =9

Array elements are 0 2 5 7 4 6 10 20 -10

Result is 3 [(0, 10), (4, 6), (20, -10)]

Input Format

First line of input is Sum

Second of input is No.of elements N,

Third line of input is, N elements separated by a single space.

Output Format

Output the number of pairs.


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

N <= 50

Sample Test cases:

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
Your Output (stdout)
No response
Response
Case2

Input (stdin)

1
20
Expected Output (stdout)

2
3
5
7
11
13
17
19
Your Output (stdout)

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

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


Factorial of a number
Case1

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;

printf("enter number: ");

scanf("%d",&n);

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

fact = fact*i;

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

return 0;

Q.13Write a program to convert decimal to octal

a program to convert decimal to octal


CASE1

Input (stdin)

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


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

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;

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


Your Output (stdout)

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


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

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

printf("element second high is: %d",sec_high);

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

int arr[] = {3,5,1,2,10,17,55};

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)

printf("element 1 not found");

else

printf("ele1 is found at position %d",f1);

if(f2==-1)

printf("element 2 not found");

else

printf("ele2 is found at position %d",f2);

return 0;

Q.16)Program to validate IP address.

Get an IP address and check whether valid or invalid.

Case1
Input (stdin)

10.0.14.255

Expected Output (stdout)

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


Valid

Your Output (stdout)

No response

Response
Case2
Input (stdin)

10.132.14.256

Expected Output (stdout)

Invalid

Your Output (stdout)

No response

Response
Case3
Input (stdin)

123.1.3.0

Expected Output (stdout)

Valid

Your Output (stdout)

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

printf("enter ip address: ");

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

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

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

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

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


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

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


Response

Solution:

#include <stdio.h>

void print3largest(int arr[], int arr_size)

int i, first, second, third;

if (arr_size < 3)

printf(" Invalid Input ");

return;

third = first = second = -1;

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

if (arr[i] > first)

third = second;

second = first;

first = arr[i];

else if (arr[i] > second)

third = second;

second = arr[i];

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


else if (arr[i] > third)

third = arr[i];

printf("Third largest element is %d\n",third);

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


****
***
**
*
Your 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)

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


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

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

You might also like