0% found this document useful (0 votes)
40 views

TCS Coding Model Paper

The document contains 6 coding questions and their solutions in C programming language. The questions include programs to check if a string is a palindrome, print unique elements of an array, calculate the factorial of a number, find the area of a circle, and find the intersection point of two lines. The solutions provided utilize basic concepts like loops, functions, conditional statements, and mathematical operations.

Uploaded by

Scribd Jdj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

TCS Coding Model Paper

The document contains 6 coding questions and their solutions in C programming language. The questions include programs to check if a string is a palindrome, print unique elements of an array, calculate the factorial of a number, find the area of a circle, and find the intersection point of two lines. The solutions provided utilize basic concepts like loops, functions, conditional statements, and mathematical operations.

Uploaded by

Scribd Jdj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

TCS Ninja - Coding_2

Total number of question :1


Test duration (min) : 20 min
Correct attempt (mark) : NA
Wrong attempt (mark) : NA

CODING

Program to check string palindrome.

#include <stdio.h>

#include <string.h>

int main(){

char string1[20];

int i, length;

int flag = 0;

scanf("%s", string1);

length = strlen(string1);

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

if(string1[i] != string1[length-i-1]){

flag = 1;

break;

if (flag) {

printf("%s is not a palindrome", string1);

else {

printf("%s is a palindrome", string1);

return 0;

______________________________________________________________________________________________________
Page 1 of 1
TCS Ninja - Coding_3

Total number of question :1


Test duration (min) : 20 min
Correct attempt (mark) : NA
Wrong attempt (mark) : NA

CODING

Program to print all the unique elements in the array.


#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE], freq[MAX_SIZE];
int size, i, j, count;
scanf("%d", &size);
// Get the N elements as input
// Assign the array freq[] as -1 (i.e., as default value)
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
freq[i] = -1;
}
// Check the frequency of all the elements in an array
for(i=0; i<size; i++)
{
count = 1;
for(j=i+1; j<size; j++)
{
if(arr[i] == arr[j])
{
count++;
freq[j] = 0;
}
}
// Store the number of occurrences of each element of the given array
if(freq[i] != 0)
{
freq[i] = count;
}
}
// Print only the unique elements
// If freq[i] value is 1, then it has occured only one time
for(i=0; i<size; i++)
{
if(freq[i] == 1)
{
printf("%d ", arr[i]);
}
}
return 0;
}
______________________________________________________________________________________________________
Page 1 of 1
TCS Ninja - Coding_4

Total number of question :1


Test duration (min) : 20 min
Correct attempt (mark) : NA
Wrong attempt (mark) : NA

CODING

Write a program to find the Factorial of the given number

#include <stdio.h>
int main()
{
long int fact=1;
int i,num;
//printf("\nPlease enter a number to find factorial : ");
scanf("%d",&num);

if (num<0)
{
//printf("\nPlease enter a positive number to");
return 1;
}
for(i=1;i<=num;i++)
{
fact=fact*i;
}
printf("%ld\n",fact);
return 0;
}

______________________________________________________________________________________________________
Page 1 of 1
TCS Ninja - Coding_5

Total number of question :1


Test duration (min) : 20 min
Correct attempt (mark) : NA
Wrong attempt (mark) : NA

CODING

Write a Program to find the area of a circle.

#include<stdio.h>
#define PI 3.14
int main()
{
float d, area;
scanf("%f",&d);
area=PI*(d/2)*(d/2);
printf("%0.2f", area);
return 0;
}

______________________________________________________________________________________________________
Page 1 of 1
TCS Ninja - Coding_6

Total number of question :1


Test duration (min) : 20 min
Correct attempt (mark) : NA
Wrong attempt (mark) : NA

CODING

Given two lines, write Program for finding its intersection point

#include <stdio.h>

int main()
{
float x1, y1, x2, y2;
scanf("%f,%f\n%f,%f", &x1, &y1, &x2, &y2);

float x3,y3,x4,y4;
scanf("%f,%f\n%f,%f", &x3, &y3, &x4, &y4);

float m1 = (y2-y1)/(x2-x1);
float m2 = (y4-y3)/(x4-x3);

// Line 1
// y = y1 + m * (x - x1);
// y = y1 + m * (x - x1);
// y = m1*x + y1 - m1 * x1

// Line 2
// y = y1 + m * (x - x1);
// y = y3 + m2 * (x - x3);
// y = m2*x + y3 - m2 * x3

// Solving line 1 and line 2 eqn


float x_intersection = (y1 - m1 * x1 - y3 + m2 * x3)/(m2-m1);
float y_intersection = y1 + m1 * (x_intersection - x1);

printf("%0.2f,%0.2f", x_intersection, y_intersection);

return 0;
}

______________________________________________________________________________________________________
Page 1 of 1

You might also like