SRM Institute of Science and Technology College of Engineering and Technology School of Computing Department of Computing Technologies

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

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.

NOTE:
● For each 5 mark question, 2 marks logic (as seen in program irrespective of syntax errors) and 3
marks for program.
● Based on number of syntax errors in program, program marks (3) may be allotted.
● There is no need to write #inlcude<stdio.h> for all programs
● There are many ways of writing the same program!
Q. No Question Marks

1a (i) Problem:
Ramesh took an examination two times. In the first attempt, he scored X marks while in the second attempt 5
he scored Y marks. According to the rules of the examination, the best score out of the two attempts will be
considered as the final score. Determine the final score of the Ramesh.
Solution:

#include<stdio.h>

int main() {

// your code goes here

int t;

scanf("%d",&t);

while(t--)

int x,y;

scanf("%d %d",&x,&y);

if(x>=y)

printf("%d\n",x);

else

printf("%d\n",y);

return 0;

Input:
4
40 60
67 55
50 50 5
1 100
Output:
4
40 60
67 55
50 50
1 100

(ii) Problem:
1. Infer and validate a program to read two numbers and print the sum of the numbers when they are different. If
both numbers are the same, double the sum and then print.

Solution:
#include <stdio.h>
int main()
{
int a,b,sum;
printf("Enter Two Numbers");
scanf("%d%d",&a,&b);
if(a==b)
{
sum=a+b;
sum=sum*2;
}
else
{
sum=a+b;
}
printf("sum = %d",sum);

return 0;
}

(iii) Problem:

Arun wants to appear in a competitive exam. To take the exam, there are following requirements:

● Minimum age limit is X (i.e. Age should be greater than or equal to X).
● Age should be strictly less than Y. 5

Arun’s current Age is A. Find whether he is currently eligible to take the exam or not.

Input Format

● First line will contain T, number of test cases. Then the test cases follow.
● Each test case consists of a single line of input, containing three integers X, Y, and A as mentioned
in the statement.

Output Format

· For each test case, output YES if Arun is eligible to give the exam, NO otherwise.

Solution

#include <stdio.h>

int main(void) {

int T;

scanf("%d", &T);

while(T--)

int X,Y,A;

scanf("%d %d %d", &X, &Y, &A);

5
if(A>=X && A<Y)
printf("YES\n");

else

printf("NO\n");

return 0;

(iv) Problem:
2. The Chessboard Distance for any two points (X1​,Y1​) and (X2​,Y2​) on a Cartesian plane is defined
as max(∣X X2​∣,∣Y Y2​∣). You are given two points (X1​,Y1​) and (X2​,Y2​). Output their Chessboard Distance.
Note that, |P| denotes the absolute value of integer P. For example, |-4| = 4∣ and ∣7∣=7.
Input Format:
● A single line of input containing 4 space separated integers X1​,Y1​,X2​,Y2​ - as defined in the problem
statement.
Output Format:
For each test case, output in a single line the chessboard distance between (X1​,Y1​) and
(X2​,Y2​)
Solution:
#include <stdio.h>
#include <stdlib.h>

int main(void) {
int t;
scanf("%d",&t);
while(t--) {
int x1,y1,x2,y2,a,b;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
a=(abs(x1-x2));
b=(abs(y1-y2));
if(a>b)
printf("%d\n",a);
else
printf("%d\n",b);
}
return 0;
}
Input:
3 5
2451
5553
1433
Output:
3
2
2
(v) Problem:
There are N different types of colors numbered from 1 to N. Ken has Ai​ balls having color i. i N). Ken
will arrange some boxes and put each ball in exactly one of those boxes. Find the minimum number of
boxes he needs so that each box contains the same color.

Input Format

The description of the test cases follows.

The first line of each test case contains a single integer N, denoting the number of colors.

The second line of each test case contains N space-separated integers A1​,A2​,…,AN​ denoting the number of
balls having color i.

Output Format

For each test case, output the minimum number of boxes required so that each box contains the same color.

You need minimum N boxes.

#include <stdio.h>

int main(void) {

int x;

scanf("%d",&x);

while(x--)

{
5
int n;

scanf("%d",&n);

int temp,i,a[n];

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

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

printf(“%d\n”, n);

return 0;

(OR)
3. (i) Problem:
4. Mohan was chatting with his friend who was a mathematician. He said "Hi !". His friend replied that '!' is the
symbol of factorial. Mohan had never heard about it and he asked more about it. Then his friend taught him
how to calculate the factorial of a number. Mohan loved that But as always he got tired after calculating a few
values and asked you to do it for him.
5. Input Format:
6. N : Number of inputs
7. then N lines with input T
8. N<10
9. T<=200
10. Output Format:
11. The result for the corresponding value of T

Solution:
#include <stdio.h>

int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
if(number<10)
{
for(i=1;i<=number;i++){
fact=fact*i;
1b }
if(fact<200)
printf("Factorial of %d is: %d",number,fact); 5
else
printf("output exceeds 200");
}
else
{
printf("Enter Lessthan 10");
}

return 0;
}

12. (ii) Problem:


Write a program to read two integer values for the variables first_number, second_number and calculate
the difference between first_number and the second_number (first_number - second_number). If the
value of the difference is between -25 and 25 (both not included) then double the value and print it, else print
the difference as it is.
Input & output Format:
For example, if 100 and 50 are given, the program should print 50 (since 100-50 = 50, which is not between -
25 and 25). If 15 and 7 are given, the program should print 16 (since the difference of 15 and 7 is 8 which is
between -25 and 25). At the time of execution, the program should print the message on the console as:
Input: 100 90
Output: 20

#include <stdio.h>
void main()
{
int a, b, c, d;
printf("Enter 2 integer values : ");
scanf("%d%d", &a, &b);
c=a-b; 5
if((c>-25) &&(c<25))
{
d = (2*c);
printf("%d", d);
}
else
{
printf("%d", c);
}
printf("\n");
}

(iii) Problem:
13. Infer and validate a program to read two integer values and if one of the numbers is within the range
of 25 to 35 (both inclusive), print Within range, otherwise print Out of range. At the time of execution, the
program should print the message on the console
Input Format:
10 20
Output Format: 5
within range
out of range
program
#include<stdio.h>
void main()
{int a,b;
printf("Enter two integer values : ");
scanf("%d%d", &a,&b);
if((a>=25 && a<=35) || (b>=25 && b<=35))
{printf("Within range\n");}
else
{printf("Out of range\n");}
}
(iv) Problem:
14. Rajesh wanted to declare an array as a block of sequential data and he was printing only addresses of the
array elements using a pointer. Help rajesh to do as described and achieve to print the address.
i/p & output:
&x[0] = 1450734448
&x[1] = 1450734452
&x[2] = 1450734456
&x[3] = 1450734460
Address of array x: 1450734448
Solution:
#include <stdio.h> 5
int main() {
int x[4];
int i;

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


printf("&x[%d] = %p\n", i, &x[i]);
}

printf("Address of array x: %p", x);

return 0;
}

Note: %d will also work


(v) Problem:
Ram is eagerly waiting for a piece of information. His secret agent told him that this information would be
revealed to him after K weeks. X days have already passed and ram is getting restless now. Find the number
of remaining days ram has to wait for, to get the information. It is guaranteed that the information has not
been revealed to the ram yet.
Input Format
The first and only line of each test case contains two space-separated integers K and X, as described in the
problem statement.
Output Format
For each test case, output the number of remaining days that ram will have to wait for.

#include <stdio.h>

int main(void) {
// your code goes here
int t;
scanf("%d",&t);
while(t--)
{
int k,x,z;
scanf("%d%d",&k,&x);
z=k*7-x;
printf("%d\n",z);
}
return 0;
}
2a (i) Problem:
15. The citizens of Byteland regularly play a game. They have blocks each denoting some integer from 0 to 9. 5
These are arranged together in a random manner without seeing to form different numbers keeping in mind
that the first block is never a 0. Once they form a number they read in the reverse order to check if the
number and its reverse is the same. If both are the same then the player wins. We call such
numbers palindrome.
Ash happens to see this game and wants to simulate the same on the computer. As the first step he wants to
take an input from the user and check if the number is a palindrome and declare if the user wins or not.
Input format:
This is followed by T lines containing an integer N.
Output Format:
For each input output "wins" if the number is a palindrome and "loses" if not, in a new line.
Constraint
1<=T<=20
1<=N<=20000
Input:
3
331
666
343
Output:
loses
wins
wins
Solution:

NOTE: Text-based solution is also correct.


#include <stdio.h>

int main(void)
{
int t;
scanf("%d",&t);
while(t--)
{
int x,n,r,y=0;
scanf("%d",&x);
n=x;
while(x!=0){

r=x%10;
y=y*10+r;
x=x/10;
}
if(n==y)
printf("wins\n");
else
printf("loses\n");
}
return 0;
}
5
(ii) Problem:
16. Trisha reads a string from the standard input device with respect to various scenario. She also finds a loop to
find the length of the string and also, she finds a loop to interchange the characters from first to last of the
string. Finally she display the reverse of a string.
Input
software
Output
Erawtfos
Solution:
#include<stdio.h>
void main() {
char ch[80], temp;
int i, j;
printf("Enter a string : ");
scanf("%s", ch);
i = j = 0;
while (ch[j] != '\0' ) { // Write the condition part
j++;
}
j--;
while ( i<=j ) { // Write the condition part
temp = ch[i]; // Complete the statement
ch[i] = ch[j] ; // Complete the statement
ch[j] = temp ; // Complete the statement
i++;
j--;
}
printf("The reverse of a given string : %s\n", ch);
} 5

(iii) Problem:
17. Rajesh invoked a code to search the occurrence of a given character in each string. He reads a string and a
character from the standard input device and write a loop to check each character of the string with a given
character. If the given character is equal to a character in the string, then increment the count with in the loop.
Finally, rajesh display the count variable which has the total number of occurrences of the given character.

Input :
Enter a string: CurrencyDemonitisation
Enter the character to be searched: n
Output:
Occurence·of·character·'n'·in·the·given·string·
CurrencyDemonitisation·=·3

Solution:
#include <stdio.h>
void main() {
char str[20], ch;
int count = 0, i;
printf("Enter a string : ");
scanf("%s", str);
printf("Enter the character to be searched : ");
scanf(" %c", &ch);
for ( i = 0; str[i] !=0; i++) { // Complete the code in for
if ( str[i] == ch ) { // Write the condition part
count++;
}
}
if (count ==0 ) { // Write the condition part
printf("The character '%c' is not presented in the string %s\n", ch, str);
} else {
printf("Occurence of character '%c' in the given string %s = %d\n", ch, str, count);
} 5
}

(iv) Problem:
18. Femina needs to concatenates two given strings and store the result in another string. Help femina to
concatenate two strings from the standard input device and write a loop to copy each character of the first
string into third string till the end of the first string. She also wants to copy each character of the second string
into third string till the end of second string.

Input:
Enter the first string·:·Rajesh
Enter the second string·: Kanna
Output:
The concatenated string·= RajeshKanna
Solution:
#include <stdio.h>
void main() {
char a[20], b[20], c[20];
int i, j;
printf("Enter the first string : ");
scanf("%s", a);
printf("Enter the second string : ");
scanf("%s", b);
for ( i = 0; a[i] != '\0'; i++ ) { // Complete the code in for
c[i] = a[i] ; //Complete the statement
}
for ( j = 0; b[j] != '\0'; j++ ) { // Complete the code in for
c[i] =b[j] ; //Complete the statement
i++;
} 5
c[i] ='\0' ; //Complete the statement
printf("The concatenated string = %s\n", c);
}

(v) Problem:
19. Kumar has a string S with him. Kumar is happy if the string contains a contiguous substring of
length strictly greater than 2 in which all its characters are vowels. Determine whether Kumar is happy or
not. Note that, in english alphabet, vowels are a, e, i, o, and u.
Input Format
● A single line of input, a string S.
Output Format
For each test case, if Kumar is happy, print HAPPY else print SAD.
You may print each character of the string in uppercase or lowercase (for example, the
strings hAppY, Happy, haPpY, and HAPPY will all be treated as identical).

Solution:
#include <stdio.h>
int main(void)
{
int t,i;
char s[1000];
scanf("%d",&t);
while(t--)
{
int c=0;
scanf("%s",s);
for(i=0;s[i]!='\0';i++)
{
if((s[i]=='a' || s[i]=='e'|| s[i]=='i' || s[i]=='o' || s[i]=='u') && (s[i+1]=='a' || s[i+1]=='e'|| s[i+1]=='i' ||
s[i+1]=='o' || s[i+1]=='u') && (s[i+2]=='a' || s[i+2]=='e'|| s[i+2]=='i' || s[i+2]=='o' || s[i+2]=='u'))
{
c++;
}
}
c>0?printf("happy \n"):printf("sad \n");
}
return 0;
}
2b
5

(OR)
(i) Problem:
20. Bob needs to find the factorization of a number into its prime factors. For example, the factorization of 20 is
2, 2, 5 and the factorization of 56 is 2, 2, 2, 7. Write a function that enables the factorization of a number into
its prime factors. Hint: Start from 2, keep trying 2 until 2 is no longer a factor, then proceed to 3 and keep
trying 3 until it is no longer a factor, then take next number and so on. Assume existence of a function
21. double sqrt(int);
22. which returns square root of a number.
Solution:
// Program to print all prime factors
# include <stdio.h>
# include <math.h>
// A function to print all prime factors of a given number n
void primeFactors(int n)
{
// Print the number of 2s that divide n
while (n%2 == 0)
{
printf("%d ", 2);
5
n = n/2;
}
// n must be odd at this point. So we can skip
// one element (Note i = i +2)
for (int i = 3; i <= sqrt(n); i = i+2)
{
// While i divides n, print i and divide n
while (n%i == 0)
{
printf("%d ", i);
n = n/i;
}
}
// This condition is to handle the case when n
// is a prime number greater than 2
if (n > 2)
printf ("%d ", n);
}

/* Driver program to test above function */


int main()
{
int n = 315;
primeFactors(n);
return 0;
}

(ii) Problem:
5
23. Monish needs to reverse the given sentence based on the value framed structure. Help monish to reverse the
sentence from the standard input and help monish to design a program to show the reverse sentence from the
actual input using function. Do not use any predefined string function. Rather, define your own function
using a pointer argument that reverses the sentence.
Input:
Enter a sentence: margorp emosewa
Output:
awesome program

Solution:
#include <stdio.h>
int main()
{
char str1[50];
char revstr[50];
char *stptr = str1;
char *rvptr = revstr;
int i=-1;
printf("\n\n Pointer : Print a string in reverse order :\n");
printf(" Input a string : ");
scanf("%s",str1);
while(*stptr)
{
stptr++;
i++;
}
while(i>=0)
{ 5
stptr--;
*rvptr = *stptr;
rvptr++;
--i;
}
*rvptr='\0';
printf(" Reverse of the string is : %s\n\n",revstr);
return 0;
}

(iii) Problem
Generally, it is not possible to change the value of a constant declaration. Mona wants to change the value of
an integer constant. Use pointer logic to implement this change
Input/output:
Before changing - value of a: 10
After changing - value of a: 20

#include <stdio.h>

int main()
{
const int a=10; //declare and assign constant integer
int *p; //declare integer pointer
p=&a; //assign address into pointer p 5

printf("Before changing - value of a: %d",a);

//assign value using pointer


*p=20;

printf("\nAfter changing - value of a: %d",a);


printf("\n value has changed.");

return 0;
}
(iv) Problem
24. Vishal needs to find the positive numbers 1, 2, 3... are known as natural numbers. Help vishal to design a
program which takes a positive integer from the user and calculates the sum up to the given number using the
function.
Input:
Enter a positive integer: 20
Output:
Sum = 210
Solution:
Non-recursive solution may also be given.
#include <stdio.h>
int addNumbers(int n);
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Sum = %d", addNumbers(num));
return 0;
}
int addNumbers(int n) {
if (n != 0)
return n + addNumbers(n - 1);
else
return n;
}

(v) Problem:
25. Reshma is a 12 year old girl playing a game of swap the two numbers A & B and write them on a board in
correct order. She is in confusion of doing this. So help her by taking this two number A and B as input and
after Swapping print the output on screen using pointers and functions.
Input Format
Input line contains two numbers A and B.
Output Format
Output in a single line the two numbers after swap them.
Solution:
int main()
{
int num1 = 5, num2 = 10;

// address of num1 and num2 is passed


swap( &num1, &num2);

printf("num1 = %d\n", num1);


printf("num2 = %d", num2);
return 0;
}

void swap(int* n1, int* n2)


{
int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
}

*Performance Indicators are available separately for Computer Science and Engineering in AICTE
examination reforms policy.

Course Outcome (CO) and Bloom’s level (BL) Coverage in Questions

BL Coverage (%)
30
30
40

BL1 BL2 BL3

Approved by the Audit Professor/Course Coordinator

You might also like