23CS101 - Problem Solving Using C++ Lab Question Paper
23CS101 - Problem Solving Using C++ Lab Question Paper
Questions
Set 1 (Medium)
Q1) Digit Sum
Write a program to implement the following logic using inheritance. Create a parent class and
implement the fun method. In the method, get the individual digits of the entered number, store it in
an array, and find their sum. Create the main class that inherits the parent class and call the fun
method inside the parent function.
Example
Input
1234
Output
30
Explanation
For 1234, the individual digits are 4,3,2,1 and the final sum →
(4+3)+(4+2)+(4+1)+(3+2)+(3+1)+(2+1) = 30.
Input Format
The input consists of an integer.
Output Format
The output prints the final sum.
Code Constraints
Integers only.
Sample 1 Input
1234
Sample 1 Output
30
Sample 2 Input
4356
Sample 2 Output
54
Input Format
First-line represents the number of elements(N) followed by the elements separated by a
single space. If the number of the elements exceeds 2 or 3, then display a message as Invalid
Input.
Output Format
Display the sum, if there are two integers or Displays product, if there are three integers.
Code Constraints
N > 0 and N < 4
Sample 1 Input
3123
Sample 1 Output
6
Sample 2 Input
2 14 56
Sample 2 Output
70
Sample 3 Input
4 67 89 43 21
Sample 3 Output
Invalid Input
Input Format
The input consists of an Integer.
Output Format
The output prints the result.
Code Constraints
Sample 1 Input
5
Sample 1 Output
100
Sample 2 Input
4
Sample 2 Output
80
Sample 3 Input
5
Sample 3 Output
100
Input Format
Input consists of four characters separated by space.
Output Format
The output prints the total score.
Code Constraints
Sample 1 Input
AFKR
Sample 1 Output
Score : 40
Sample 2 Input
AbDf
Sample 2 Output
Score : 10
In the main method, prompt the user to enter the power rate of the appliance and the total hours used
then create the necessary objects and call the methods.
Input Format
The first line consists of the power rating of the fan and the total hours used separated by
space. The second line consists of the power rating of Light and the total hours used separated
by space. The third line consists of the power rating of the TV and the total hours used
separated by space.
Output Format
The output prints the bill amount. Refer to the sample input and output for formatting
specifications.
Code Constraints
Sample 1 Input
40 123
55 200
33 400
Sample 1 Output
43.68
Sample 2 Input
60 300
54 360
30 720
Sample 2 Output
88.56
Q6) You are required to compute the power of a number by implementing a calculator. Create a class
MyCalculator which consists of a single method long power(int, int). This method takes two integers,
n and p, as parameters and finds np. If either n or p is negative, then the method must throw an
Exception that says "n and p should be non-negative". Also, if both n and p are zero, then the method
must throw an Exception that says “ n and p should not be zero”. Complete the function power in
class MyCalculator and return the appropriate result after the power operation or an appropriate
exception as detailed above.
Input Format
n value as an integer in the first line p value as an integer in the second line
Output Format
Calculated power value or appropriate exception
Code Constraints
Sample 1 Input
3
4
Sample 1 Output
81
Sample 2 Input
3
-4
Sample 2 Output
n and p should be non-negative
Sample 3 Input
0
5
Sample 3 Output
0
Sample 4 Input
0
0
Sample 4 Output
Both n and p should not be zero
Q7) Write a program to get two time periods which are stored using structure and write it to file.
Read them from file to compute the difference between them.
Input Format
Hours, minutes and seconds of start time in first line separated by space Hours, minutes and
seconds of stop time in second line separated by space
Output Format
Difference between start time and stop time
Code Constraints
Sample 1 Input
10 20 30
8 10 25
Sample 1 Output
2:10:5
Sample 2 Input
10 20 30
8 10 35
Sample 2 Output
2:9:55
Output Format
The output prints the modified/original array and the exception. Refer sample input and
output for formatting specifications.
Code Constraints
Sample 1 Input
5
12 85 32 45 65
3 120
Sample 1 Output
12 85 32 120 65
Sample 2 Input
5
12 85 32 45 65
6 88
Sample 2 Output
Index out of bounds
12 85 32 45 65
Input Format
Two lines of input. First-line contains the number of elements. and the second line contains
the elements of the array.
Output Format
Output is displayed as shown in sample test cases.
Code Constraints
Sample 1 Input
5
cdthf
Sample 1 Output
Before sorting: c d t h f
After sorting: c d f h t
Sample 2 Input
8
rfgvdsew
Sample 2 Output
Before sorting: r f g v d s e w
After sorting: d e f g r s v w
Input Format
The first line of the input consists of the value of n. The next line of input is the list of
elements.
Output Format
The output prints the even elements in the list/no even elements in the list. Refer sample input
and output.
Code Constraints
Sample 1 Input
5
13 17 41 47 99
Sample 1 Output
The list contains no even numbers.
Sample 2 Input
5
13 17 42 46 99
Sample 2 Output
The first even number in the list is 42.
Q11) Fibonacci series starts from 0,1 and the next number should be the sum of the previous two
numbers.
• 0+1=1, so the next number in the Fibonacci series in the c program is 1.
• 0 1 1 2 3 5 8.. and so on.
Write a program to generate Fibonacci series up to n terms using while loop
Input Format
The input consists of the value of n.
Output Format
The output prints the Fibonacci series.
Code Constraints
Sample 1 Input
10
Sample 1 Output
0 1 1 2 3 5 8 13 21 34
Q12) Write a program to print the following pattern.
7654321
765432
76543
7654
765
76
7
Input Format
Number of rows
Output Format
Print the pattern as shown in sample output
Code Constraints
Sample 1 Input
4
Sample 1 Output
4321
432
43
4
Set 2 (Hard)
Mukesh and his friends have set out on a vacation to Coorg. They have booked accommodation in a
resort and the resort authorities organize Campfires every night as a part of their daily activities.
Mukesh volunteered himself for an activity called the "Adjacent Stick Game" where sticks of
different lengths will be placed in a line and Mukesh needs to remove a stick from each adjacent pair
of sticks. He then has to form a bigger stick by combining all the remaining sticks.
Mukesh needs to know the smallest length of the bigger stick so formed and needs your help to
compute the same. Given the number of sticks N and the lengths of each of the sticks, write a program
to find the smallest length of the bigger stick that is formed.
Input Format
The first line of input contains an integer N denoting the number of sticks. Assume that the
maximum value for N is 50. Assume that N is always even. The next line of input contains an
N integer denoting the length of each of the sticks.
Output Format
Output the smallest length of the bigger stick that is formed.
Code Constraints
Sample 1 Input
4
2131
Sample 1 Output
2
Sample 2 Input
4
2541
Sample 2 Output
3
Input Format
The first line of input contains an integer N, denoting the number of elements in the array.
The next line of input contains N values, each of which is either 1, 2, or 3.
Output Format
The output prints the minimum number of rooms that need to be painted in order to have all
the rooms painted with the same color i.e red, blue, or green
Code Constraints
Sample 1 Input
3
121
Sample 1 Output
1
Sample 2 Input
3
123
Sample 2 Output
1
Given n the number of rows of seats and the number of seats in a row and the ages of passengers in
each seat can you find the number of mid-aged passengers seated in the diagonals.
Input Format
The first line input consists of an integer n, corresponding to the number of rows of seats and
the number of seats in the aircraft. The next n lines of input consist of n integers that
correspond to the ages of passengers
Output Format
The output consists of an integer corresponding to the number of mid-aged passengers seated
in the diagonals.
Code Constraints
Sample 1 Input
3
25 17 20
33 26 30
473
Sample 1 Output
2
Sample 2 Input
5
2 30 34 14 18
80 8 36 44 21
20 23 24 25 25
18 18 19 17 28
29 28 10 12 56
Sample 2 Output
2
Q4) Number of White Cells
Nurikabe logical game (sometimes called Islands in the Stream) is a binary determination puzzle. The
puzzle is played on a typically rectangular grid of cells, some of which contain numbers. You must
decide for each cell if it is white or black (by clicking on them) according to the following rules:
• All of the black cells must be connected.
• Each numbered cell must be part of a white island of connected white cells.
• Each island must have the same number of white cells as the number it contains (including
the numbered cell).
• Two islands may not be connected.
• There cannot be any 2x2 blocks of black cells.
Unnumbered cells start out grey and cycle through white and black when clicked. Initially numbered
cells are white in color.
Problem Statement:
Write a program to find the number of white cells in the final configuration of the board, given a valid
initial configuration. Below figure is the sample valid initial configuration.
Input Format
The first line of the input is an integer N that gives the number of rows and columns of the
grid. Next N lines will have a valid initial board configuration with N*N cells. Assume that
the maximum number in a cell can be 10. Grey-colored cells are represented by the integer
20 in the matrix representation of the input configuration.
Output Format
The output should display an integer that the number of white cells in the final configuration
of the board.
Code Constraints
Sample 1 Input
5
20 20 1 20 3
20 20 20 20 20
20 20 20 20 20
20 20 20 20 20
6 20 3 20 20
Sample 1 Output
13
Sample 2 Input
4
20 20 1 20
20 20 20 20
8 20 20 20
20 20 20 20
Sample 2 Output
9
After waiting in long queues, Dhilip succeeded in getting the tickets for the big match. On the ticket,
there is a letter code that can be represented as a string of upper-case Latin letters.
Dhilip believes that the CSK Team will win the match in case exactly two different letters in the code
alternate. Otherwise, he believes that the team might lose. Please see the note section for the formal
definition of alternating code.
You are given a ticket code. Please determine, whether CSK Team will win the match or not based on
Dhilip’sconviction. Print "YES" or "NO" (without quotes) corresponding to the situation.
Note:
Two letters x, y where x != y are said to be alternating in a code if the code is of the form "xyxyxy...".
Input Format
The first and only line of the input contains a string S denoting the letter code on the ticket.
Output Format
Output a single line containing "Yes" (without quotes) based on the conditions given and
"No" otherwise.
Code Constraints
Sample 1 Input
ABABAB
Sample 1 Output
Yes
Sample 2 Input
ABC
Sample 2 Output
No
Input Format
The first line of the input contains the string ‘X’. The second line of the input contains the
string ‘Y’.
Output Format
Output a single line with the word "Yes"(without quotes) if the strings can be matched,
otherwise output "No"(without quotes).
Code Constraints
Sample 1 Input
s?or?
sco??
Sample 1 Output
Yes
Sample 2 Input
s?orc?
scod??
Sample 2 Output
No
Input Format
The first line of the input consists of the value of n. The next n inputs are the strings to be
sorted.
Output Format
The output prints the sorted strings.
Code Constraints
Sample 1 Input
5
adc
abc
aaa
bdc
bbb
Sample 1 Output
1 aaa
2 abc
3 adc
4 bbb
5 bdc
The much-awaited event in the entertainment industry every year is the "Screen Awards". This year
the event is going to be organized on December 25 to honor the Artists for their professional
excellence in Cinema. The Organizers of the event, J&R Events, decided to design the logo of the
Screen Awards as a digitized image and display it on the LED panel boards for the show promotions
all across the venue. The Event team wanted to border the logo with right triangles which will
describe it better.
For this purpose, the Event development team is in the task to find if N dots can make a right triangle
or not (all N dots must be used). Given N dots, we can make it look like a Right Triangle (45-45-90
triangle) exactly with N dots. Rearrange the given N dots, like this:
Your task is to help the team write a program using functions to find if N dots can make a right
triangle or not.
Function Specifications:
Use the function name, return type, and the argument type as:
int find(int)
The function must return 1 if you can make a right triangle using N dots, else return 0.
Note:
The main function is already provided and well defined. The function mentioned above is to be
defined by you to solve this problem
Input Format
The first line of the input consists of an integer N.
Output Format
Output "Yes" (without quotes) if you can make a right triangle using N dots, otherwise
"No"(without quotes). Refer sample input and output for formatting specifications.
Code Constraints
Sample 1 Input
6
Sample 1 Output
We can create Right Triagle of dots with 6 dots
Sample 2 Input
4
Sample 2 Output
We can't create Right Triagle of dots with 4 dots
Input Format
Input to get two values W, L denoting the Weight and the Length. [W is an integer. L is a
decimal value].
Output Format
print the BMI Grade as U, N, H, or O.
Code Constraints
Sample 1 Input
200 2.8
Sample 1 Output
H
Sample 2 Input
129 3.4
Sample 2 Output
U
Input Format
The first input consists of the n value. The next n inputs are the roll numbers and five marks.
Output Format
The output prints the roll number, five marks of the subject, average, and grade of the student.
Refer to the sample input and output for the formatting specifications.
Code Constraints
Sample 1 Input
2
101
85 78 89 76 67
102
58 69 47 69 84
Sample 1 Output
101 85 78 89 76 67 79 1
102 58 69 47 69 84 65 2
Sample 2 Input
1
122
25 23 31 28 29
Sample 2 Output
122 25 23 31 28 29 27 3
Sample 3 Input
3
1
84 85 96 97 80
2
65 72 52 46 55
3
24 26 30 15 30
Sample 3 Output
1 84 85 96 97 80 88 1
2 65 72 52 46 55 58 2
3 24 26 30 15 30 25 3
Input Format
The first line of the input consists of the value of n. Next n inputs consist of the item code,
brand name, item name, quantity, and price of the product( per item).
Output Format
The output prints a message if the final amount of the product is greater than 1000. The next
line prints the bill amount (Rounded off to two decimal places). The last line prints whether
the customer gets a voucher or not. Refer sample input and output for formatting
specifications.
Code Constraints
Sample 1 Input
2
101 philsbury flour 10 55
102 dettol soap 50 25
Sample 1 Output
soap costs more than 1000
1800
No voucher
Sample 2 Input
2
101 philsbury flour 10 55
102 dettol soap 500 25
Sample 2 Output
soap costs more than 1000
13050
You have won a voucher of Rs.200
Sample 3 Input
2
101 philsbury flour 10 55
102 dettol soap 10 25
Sample 3 Output
800
No voucher
Sample 4 Input
12
101 philsbury flour 10 55
102 dettol soap 10 25
103 ponds powder 10 99
104 lays chips 198 5
105 maaza drinks 18 50
106 nescafe coffee 12 75
107 boost powder 4 195
108 meera shampoo 498 2
109 silk shampoo 498 2
110 horlicks powder 5 195
111 sunrise coffee 19 50
112 gokul powder 20 45
Sample 4 Output
10177
You have won a voucher of Rs.200
Input Format
The first line of the input consists of the value of n. The next n inputs are the name, id, years
of experience, and salary separated by a space.
Output Format
The output prints the employee details with updated salaries. Refer sample input and output
for formatting specifications.
Code Constraints
Sample 1 Input
3
alice 101 10 40000
bob 102 8 35000
charles 103 4 25000
Sample 1 Output
Employee Name : alice
Employee Id : 101
years of experience : 10
salary : 44000
Sample 2 Input
4
alice 101 10 40000
bob 102 8 35000
charles 103 4 25000
david 104 12 50000
Sample 2 Output
Employee Name : alice
Employee Id : 101
years of experience : 10
salary : 44000