You are on page 1of 15

SRM Institute of Science and Technology

College of Engineering and Technology


School of Computing
DEPARTMENT OF COMPUTING TECHNOLOGIES
SRM Nagar, Kattankulathur – 603203, Chengalpattu District, Tamilnadu
Academic Year: 2022-23 (ODD)

Test: CLAT- 2 Date:


Course Code & Title: 21CSS101J / Programming for Problem Solving Duration: 1 hr 40 mts
Year & Sem: I / I Max. Marks: 50

Course Learning Rationale (CLR):

CLR-2 : Utilize the appropriate operators and control statements to solve engineering problems
CLR-3 : Store and retrieve data in a single and multidimensional array
CLR-4 : Create custom designed functions to perform repetitive tasks in any application

Course Learning Outcomes (CLO):

CLO-2 : To use appropriate data types in simple data processing applications. To create programs using the concept
of arrays.
CLO-3 : To create string processing applications with single and multi-dimensional arrays.
CLO-4 : To create user-defined functions with required operations. To implement pointers in applications with
dynamic memory.

Q. Question
No
1
Back to School
Raghav has figured out 3 types of bowling techniques, that could be most beneficial for dismissing Jatin. He has
given points to each of the 3 techniques.
You need to tell him which is the maximum point value, so that Raghav can select best technique.
3 numbers are given in input. Output the maximum of these numbers.
Input:
Three space separated integers.
Output:
Maximum integer value
SAMPLE INPUT
861
SAMPLE OUTPUT
8

#include <stdio.h>

int main()

int a, b, c, max;

scanf("%d %d %d", &a, &b, &c);

max = (a > b) ? (a > c ? a : c) : (b > c ? b : c);

printf("%d", max);

return 0;

}
2
Holes in a Number
You are designing a poster which prints out numbers with a unique style applied to each of them. The styling is
based on the number of closed paths or holes present in a given number. The number of holes that each of the
digits from 0 to 9 have are equal to the number of closed paths in the digit. Their values are:
1, 2, 3, 5, and 7 = 0 holes.
0, 4, 6, and 9 = 1 hole.
8 = 2 holes.
Given a number, you must determine the sum of the number of holes for all of its digits. For example, the
number 819 has 3 holes. Complete the program, it must must return an integer denoting the total number of holes
in num.

Sample Input
630
Sample Output
2

#include <stdio.h>

int main() {

int num, sum = 0, rem;

scanf("%d", &num);

while (num > 0)

rem = num % 10;

if (rem == 0 || rem == 4 || rem == 6 || rem == 9)

sum = sum + 1;

else if (rem == 8)

sum = sum + 2;

num = num / 10;

printf("%d", sum);

return 0;

3 Same Digit
Write a program to read two integer values and print true if both the numbers end with the same digit, otherwise
print false.
Example: If 698 and 768 are given, program should print true as they both end with 8.
Sample Input
25 53
Sample Output
false

#include <stdio.h>

int main()
{

int a, b;

scanf("%d %d", &a, &b);

if ((a % 10) == (b % 10))

printf("true");

else

printf("false");

return 0;

4
Pythagorean Triples
Three numbers form a Pythagorean triple if the sum of squares of two numbers is equal to the
square of the third.
For example, 3, 5 and 4 form a Pythagorean triple, since 3*3 + 4*4 = 25 = 5*5
You are given three integers, a, b, and c. They need not be given in increasing order. If they form a Pythagorean
triple, then print "yes", otherwise, print "no". Please note that the output message is in small letters.
Sample Input
3
5
4
Sample Output
yes

#include <stdio.h>

int main()

int a, b, c, d, e, f;

scanf("%d %d %d", &a, &b, &c);

d = a * a;

e = b * b;

f = c * c;

if (((d + e) == f || ((d + f) == e) || ((e + f) == d)))

printf("yes");

else

printf("no");

return 0;

}
5
Classifying Triangles
A triangle can be classified based on the lengths of its sides as equilateral, isosceles or scalene. All three sides of
an equilateral triangle have the same length. An isosceles triangle has two
sides that are the same length, and a third side that is a different length. If all of the sides have different lengths
then the triangle is scalene.
Write a program that reads the lengths of the three sides of a triangle from the user. Then display a message that
states the triangle’s type.
Sample Input
10.0
10.0
10.0
Sample Output
Equilateral

#include <stdio.h>

int main()

float side1, side2, side3;

scanf("%f %f %f", &side1, &side2, &side3);

if (side1 == side2 && side2 == side3)

printf("Equilateral");

else

if (side1 == side2 || side2 == side3 || side3 == side1)

printf("Isosceles");

else

printf("Scalene");

return 0;

}
6 (OR)

Add Alternate Elements of 2-Dimensional Array


You are given a two-dimensional 3*3 array starting from A [0][0]. You should add the alternate elements of the
array and print its sum. It should print two different numbers the first being sum of A 0 0, A 0 2, A 1 1, A 2 0, A
2 2 and A 0 1, A 1 0, A 1 2, A 2 1.
Input Format
First and only line contains the value of array separated by single space.

Output Format
First line should print sum of A 0 0, A 0 2, A 1 1, A 2 0, A 2 2 Second line should print sum of A 0 1, A 1 0, A 1
2, A 2 1 SAMPLE INPUT
123456789
SAMPLE OUTPUT
25 20

#include <stdio.h>

int main()

int a[9], i, sum1 = 0, sum2 = 0;

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

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

sum1 = a[0] + a[2] + a[4] + a[6] + a[8];

sum2 = a[1] + a[3] + a[5] + a[7];

printf("%d\n%d", sum1, sum2);

return 0;

7
Sort it out!
You are given an array A of non-negative integers of size m. Your task is to sort the array in non-decreasing
order and print out the original indices of the new sorted array.
Example:
A={4,5,3,7,1}
After sorting the new array becomes A={1,3,4,5,7}.
The required output should be "4 2 0 1 3"
INPUT :
The first line of input consists of the size of the array
The next line consists of the array of size m
OUTPUT :
Output consists of a single line of integers
NOTE: The indexing of the array starts with 0.
SAMPLE INPUT
5
45371
SAMPLE OUTPUT
42013

Basically you use an array a to store the array being sorted, and another array b to store the original locations of
elements in the modified array. Whenever there is a swap of numbers in a, you also swap the corresponding
locations in b.

#include <stdio.h>
int main()
{
int m, a[100], b[100], i, j, temp;
scanf("%d", &m);
for (int i = 0; i < m; i++)
{
scanf("%d", &a[i]);
b[i] = i;
}
for (i = 0; i < m - 1; i++)
{
for (j = 0; j < m - i - 1; j++)
{
if (a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
temp = b[j];
b[j] = b[j + 1];
b[j + 1] = temp;
}
}
}
8 for (i = 0; i < m; i++)
printf("%d ", b[i]);
return 0;
}

Suppose we have a square matrix; we have to find the sum of the matrix diagonals. So only include the sum of
all of the elements on the primary diagonal.
So, if the input is like

Then the output will be the sum of 4,5 and 7 which is 16.
Input Format
The first line contains an integer, N (the size of our array).
The second line contains N space-separated integers describing array A's elements.
Output Format
Print the sum of the diagonal elements of array A

SAMPLE INPUT
9
123456789
SAMPLE OUTPUT
15

# include <stdio.h>

void main()
{

int mat[10][10] ;

int i, j, size, sum = 0 ;

scanf("%d", &size) ;

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

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

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

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

sum = sum + mat[i][i] ;


9
printf(" %d", sum) ;

getch() ;

Write a C program to swap two integers using pointers. You have to write a swap function that will accept the
address of two integer and swap their values.
Sample Test Cases
Test Case
Input
12
45
Output
num1=45, num2=12
#include <stdio.h>

void swap(int *a,int *b)

int t;

t = *a;

*a = *b;

*b = t;

int main()

int num1, num2;

scanf("%d", &num1);

scanf("%d", &num2);

swap(&num1, &num2);

printf("num1=%d, num2=%d", num1, num2);


10
return 0;
}

Write a program in C to find the factorial of a given number using pointers.


Sample Test Cases
Test Case
Input
5
Output
The Factorial of 5 is : 120

#include <stdio.h>

void findFact(int, long int*);

int main()

long int fact;

int num1;

scanf("%d", &num1);

findFact(num1, &fact);

printf("The Factorial of %d is : %ld", num1, fact);

return 0;

void findFact(int n, long int *f)

int i;

*f = 1;

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

*f = *f * i;

11 Write a program to replace all vowels with star (*) and consonants with hash (#) of the given string.
At the time of execution, the program should print the message on the console as:
Enter a string :
For example, if the user gives the input as:
Enter a string : CodeTantra
then the program should print the result as:
The given string is: CodeTantra
The resultant string is : #*#*#*###*
Note: Do use the printf() function with a newline character (\n).
int main()

char str[50], ch, i;


printf("Enter a string : ");

gets(str);

printf("The given string is: %s\n",str);

for(i=0; str[i]!='\0'; i++)

if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o'

|| str[i]=='u' || str[i]=='A' || str[i]=='E' || str[i]=='I'

|| str[i]=='O' || str[i]=='U')

str[i] = '*';

else

str[i]='#';

printf("The resultant string is : %s",str);

return 0;

12 Write a program to convert the lowercase characters into uppercase characters of a given string.
At the time of execution, the program should print the message on the console as:
Enter any string :
For example, if the user gives the input as:
Enter any string: PPS CodeTantra
then the program should print the result as:
The given string is : PPS CodeTantra
The resultant string is : PPS CODETANTRA
Note: Do use the printf() function with a newline character (\n) at the end.
#include <stdio.h>

#include <string.h>

int main() {

char s[100];

int i;

printf("Enter any string: ");

gets(s);
printf("The given string is : %s\n",s);

for (i = 0; s[i]!='\0'; i++)

if(s[i] >= 'a' && s[i] <= 'z')

s[i] = s[i] - 32;

printf("The resultant string is : %s", s);

return 0;

13
Write a program to print indexes of a particular character in a string.
At the time of execution, the program should print the message on the console as:
Enter a string :
For example, if the user gives the input as:
Enter a string : NarendraModi
Next, the program should print the message on the console as:
Enter a character :
For example, if the user gives the input as:
Enter a character : a
then the program should print the result as:
The indexes of a character 'a' in the given string NarendraModi = 1 7
Note: Do use the printf() function with a newline character (\n) at the end.
#include<stdio.h>

#include <stdio.h>

int main()

char str[30],ch;

int ind[10],loop,j;

printf("Enter a string :");

gets(str);

printf("Enter a character : ");

ch=getchar();
j=0;

for(loop=0; str[loop]!='\0'; loop++)

if(str[loop]==ch)

ind[j++]=loop;

printf("The indexes of a character %c in the given string %s = ",ch,str);

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

printf("%d ",ind[loop]);

return 0;

14
Write a program to reverse a string without using builtin function.
At the time of execution, the program should print the message on the console as:
Enter a string :
For example, if the user gives the input as:
Enter a string : Programming
then the program should print the result as:
The reversed string is : gnimmargorP
Note: Do use the printf() function with a newline character (\n) at the end.
#include <stdio.h>

#include <string.h>

int main()

char str[100];

int i,n;

int count =0;

printf("Enter a string : ");

scanf("%s",str);

n= strlen(str);

printf("The reversed string is : ");

for(i=n;i>=0;i--)
{

printf("%c",str[i]);

15
Write a program to read two strings and print true if both the strings are the same, otherwise print false.
Sample Input 1
PPS
PPS
Sample Output 1
true
#include <stdio.h>

#include <string.h>

int main ()

//char str[10];

char str1[100];

char str2[100];

int n;

gets(str1);

gets(str2);

n= strcmp(str1,str2);

if(n==0)

printf("true");

else

printf("false");

}
16
(OR)
Prime or Not?
Given an integer, if the number is prime, return 1. Otherwise return its smallest divisor greater than 1.
Example
n = 24 The number 24 is not prime: its divisors are [1, 2, 3, 4, 6, 8, 12, 24]. The smallest divisor greater than 1 is
2.
Function Description
Complete the function isPrime in the editor below.
isPrime has the following parameter(s):
long n: a long integer to test
Returns
int: if the number is prime, return 1; otherwise returns the smallest divisor greater than 1
Input Format for Custom Testing
Input from stdin will be processed as follows and passed to the function. The only line of input contains the long
integer to analyze, n.
Sample Case
Sample Input
2
Sample Output
1

int isPrime(long n)

long i;

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

if (n % i == 0)

return i;

return 1;

17

Sum Them All


Calculate the sum of an array of integers.
Example
numbers = [3, 13, 4, 11, 9]
The sum is 3 + 13 + 4 + 11 + 9 = 40.
Function Description
Complete the function arraySum in the editor below.
arraySum has the following parameter(s):
int numbers[n]: an array of integers
Returns
int: integer sum of the numbers array
Format for Custom Testing
Input from stdin will be processed as follows and passed to the function.
The first line contains an integer n, the size of the array numbers.

Sample Case
Sample Input
5
1
2
3
4
5
Sample Output
15
int arraySum(int numbers_count, int *numbers)

int i, sum = 0;

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

sum = sum + numbers[i];

return sum;
18
}
Summation
To add the two numbers using call by reference.
Function Description
Complete the function add in the editor below.
add has the following parameter(s):
parameters:
Using call by reference
Returns
int:sum of two numbers
Input Format for Custom Testing
Input from stdin will be processed as follows and passed to the function.
The only line of input contains the long integer to analyze, n.
Sample Case 0
Sample Input 0
2 1
Sample Output 0
3
int sum(int *n1, int *n2)

int sum;

sum = *n1 + *n2;

return sum;
19 }

Given a number, the task is to find the product of the digits of a number.
Function Description
Complete the function getProduct in the editor below.
getProduct has the following parameter(s):
int n: integer to test
Returns
int: product of all the digits
Sample Test case
Input:
4513
Output:
60
int getProduct(int n)

int product = 1;

while (n != 0)

product = product * (n % 10);

n = n / 10;

20 return product;
}
Given a number, the task is to find the number of digits of a number.

Function Description
Complete the function countDigit in the editor below.
countDigit has the following parameter(s):
int n: integer to test
Returns
int: Number of digits
Sample Test case
Input:
4513
Output:
4
int countDigit(int n)

if (n == 0)

return 1;

int count = 0;

while (n != 0) {

n = n / 10;

++count;

return count;

You might also like