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

23CS101 - Problem Solving Using C++ Lab Question Paper

The document contains a set of programming lab questions focused on problem-solving using C++. It includes various tasks such as implementing inheritance, function overloading, calculating scores, handling exceptions, and working with data structures. Each question provides specific input and output formats along with sample cases for clarity.

Uploaded by

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

23CS101 - Problem Solving Using C++ Lab Question Paper

The document contains a set of programming lab questions focused on problem-solving using C++. It includes various tasks such as implementing inheritance, function overloading, calculating scores, handling exceptions, and working with data structures. Each question provides specific input and output formats along with sample cases for clarity.

Uploaded by

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

23CS101 - Problem Solving using C++ Lab

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

Q2) Problem Statement:


Function Overloading
Write a program to implement function overloading.
Ram is given two or three inputs as an integer, if he has two integers then add the two numbers. If he
has three inputs, then multiply the three numbers.
Function Header:
public void fun1(int a,int b,int c)
public void fun1(int a,int b)

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

Q3) Problem Statement:


Write a program to find that whether the given number(x) is even or odd if it is even then print the
cube(x)+square(x) else print cube(x)-square(x).
Create a base class with a virtual function void print(). print the result by implementing this virtual
function in the derived class.

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

Q4) Problem Statement:


Alphabetics Game:
You have to enter four letters for each uppercase letter you will get 10 marks and for each lowercase
letter, you will get -5 marks.
Write a program to calculate the total score.
Create a base class with a virtual method void game(). Define this method in the Derived class and
calculate the total score.

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

Q5) Problem Statement:


Write a Program to calculate the current bill.
Create a class currentBill with a virtual method double amount().
Create a Fan
Create a class Fan that extends currentBill with watts and hours as its public attributes and overrides
the virtual function.
Create a class Light that extends currentBill with watts and hours as its public attributes and overrides
the virtual function.
Create a class TV that extends currentBill with watts and hours as its public attributes and overrides
the virtual function.

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.

File Name : input.txt

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

Q8) Problem Statement


Index out of bounds exception:
Create an array of size n and add elements to it. Get a position and a value and modify the same.
If the position exceeds the size of the array, throw and catch an exception, and print "Index out of
bounds".
Input Format
The first line of the input consists of the n value. The next line of the input consists of the
values of the array elements The last line of the input consists of the position and the element
to be replaced.

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

Q9) Problem Statement:


Using the sort algorithm of STL, write a program that sorts a user-defined character array in
ascending order.

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

Q10) Problem Statement:


Write a program to create a list of integer elements and print the even elements in them using lambda
expressions.

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)

Q1) Adjacent Stick Game

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

Q2) Most occurring element

An array is filled with one of the values 1, 2, or 3.


Find the value that occurs the most in the array.
If two or more values occur the most number of times print the lower value.

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

Q3) Mid Aged


The Pan Am 73 flight from Bombay to New York en route to Karachi and Frankfurt was hijacked by
a few Palestinian terrorists at the Karachi International Airport. The senior flight purser Neerja Banhot
withered her fear and helped evacuate the passengers on board.
Neerja very well knew that she would not be able to evacuate all passengers dodging the hijackers. So
she wanted to hand over the responsibility of evacuating the senior citizens(above 60 years of age)
and children(below 18 years of age) in the flight to the mid-aged passengers seated in the diagonals

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

Q5) Problem Statement:


Alternating Code
It is IPL season and the first league match of Dhilip’s favorite team, "Chennai Super Kings". The
CSK team is playing at the IPL after 2 years and like all Dhoni lovers, Dhilip is also eagerly awaiting
to see Dhoni back in action.

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

Q6) Problem Statement:


Wildcard Matching
Sunil is a little scientist. Sunil has planned to design a wildcard pattern matcher to exhibit at the
"Young Inventors", a tech talent show organized at his school.
Sunil wanted to design the wildcard pattern matcher supporting the wildcard character '?'. The
wildcard character '?' can be substituted by any single lower case English letter for matching. He has
two strings X and Y of equal length, made up of lower case letters and the character '?'.
Sunil wants your help in designing the device, to know whether the strings X and Y can be matched
or not. Write a program to check whether the given strings can be matched or not.
Note : Print 'No' if the length of strings are not equal.

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

Q7) Write a program to sort a set of strings using pointers.

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

Q8) Problem statement:

Right Triangle of Dots

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

Q9) Problem Statement:


BMI Calculator
The simple measure of body fitness is the BMI or Body Mass Index. It depends only on the height L
and weight W of a person. It is defined as BMI = [weight / height^2] where weight is taken in
kilograms and length in meters. Four general grades are proposed:
Underweight[U] - when BMI < 18.5
Normal weight[N] - 18.5
Heavyweight [H] - 25.0
Overweight [O] - above or equal to 30.
Write a program that takes in the Weight (in Kg) and Length (in meters) of an individual and displays
the grade as U, N, H, O.
Note: Bind the data members and the member functions inside the class

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

Q10) Problem statement :


Student structure
Create a structure student with the following members
Roll Number
Five subject marks
Average
Grade
Given the five subject marks, Calculate the average and grade.
GRADE CALCULATION:
1)if avg>70 the grade will be 1
2)if avg 50 to 70 the grade will be 2
3)if avg is below 50 the grade will be 3 (Note: rn- Roll Number, s-Subjects, avg- Average)

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

Q11) Problem Statement:


In an online grocery shop, customers want to purchase multiple items. Create a structure to store the
Item code, Brand name, Item Name, Quantity, Price of the product. Generate the Bill number, Display
the purchased product, name, amount and quantity, and the total bill amount.
a. Write a function MESSAGE() to alert the customer with the product name if the rate of a product
is more than Rs.1000.
b. Write a function VOUCHER() to generate the voucher for Rs.200 if the bill amount is greater than
Rs.10000.

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

Q12) Problem Statement:


Create a structure named DEPT with the following fields: Name, emp-id, years_of_experience, and
Basic salary. Define an array of structures for ‘n’ employees and check the following constraints and
print the results.
- Increase 10% to the salaries of those employees who have worked for 10 years or more
- Increase 5% to the salaries of those employees who have experienced between 5 to 9 years.
- Increase 2% to the salaries of those employees who have experienced between 1 to 4 years.

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

Employee Name : bob


Employee Id : 102
years of experience : 8
salary : 36750

Employee Name : charles


Employee Id : 103
years of experience : 4
salary : 25500

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

Employee Name : bob


Employee Id : 102
years of experience : 8
salary : 36750

Employee Name : charles


Employee Id : 103
years of experience : 4
salary : 25500

Employee Name : david


Employee Id : 104
years of experience : 12
salary : 55000

You might also like