Question 1:
Problem Statement: The union of two sets A and B is defined as the set of all the elements
which lie in set A and set B or both the elements in A and B altogether. The intersection of two
sets A and B is the set containing all elements of A that also belong to B or equivalently, all
elements of B that also belong to A. Write a C++ program to perform union and intersection
operation on two integer array. Input array contains distinct elements.
Input:5
12345
4
4567
Output:1 2 3 4 5 6 7
45
Constraints: Both set should contains distinct elements
Question 2:
Problem Statement:A prime number is a whole number greater than 1 whose only factors are 1
and itself. Write a program take input a positive number and use recursion to check whether the
entered number is prime or not.
Input: 11
Output: PRIME
Question 3:
Problem Statement:
The factorial of a positive integer n is equal to 1*2*3*...n. Write a C++ program to find the
factorial of a positive integer.
INPUT: Any positive integer less than 16
OUTPUT: Factorial of the entered number
Input: 3
Output: 6
Input: 5
Output: 120
If you enter negative number as input it will give a message 'Error!'.
Input: -3
Output: Error!
Question 4:
Problem Statement: Write a C++ program to print the following pattern
*****
****
***
**
*
Input: 3
Output:
***
**
*
Question 5:
Problem Statement: Whole numbers in which the digits of one number are the reverse of the
digits in another number, for example, 2847 and 7482 form a reversible pair. Write a C++
program to REVERSE a number using recursion. Define a function numReverse() to reverse the
number.
Input: 2143
Output: 3412
Question 6:
Problem Statement: Write a C++ program to print the following pattern
*
**
***
****
*****
Input: 3
Output: *
**
***
Question 7:
Problem Statement: The power (or exponent) of a number says how many times to use the
number in a multiplication. Write a C++ program to calculate power of a number using
recursion. Define a function calPower() to calculate power. Take two positive integer m and n,
then calculate m^n.
Input:2 2
Output: 4
CODING QUESTION
Question 8:
Problem Statement: Write a C++ program to print the following pattern
1
22
333
4444
55555
Input:3
Output:
1
22
333
Question 9:
Problem Statement: You are given total minutes. Write a C++ program to convert the given
minutes into hours and minutes. Separate the number of hours and minutes with a colon.
Input:66
Output:1:6
Question 10:
Problem Statement:
Manoj is trying to find largest twin prime numbers less than n. A twin prime is a prime number
that is either 2 less or 2 more than another prime number, for example, either member of the twin
prime pair (41, 43). In other words, a twin prime is a prime that has a prime gap of two".
Write a C++ program that reads a given integer n and prints a twin prime that has the maximum
size among twin primes less than or equal to n.
Input:15
Output: 11 13
Question 11:
Problem Statement:
A binary number contains only 1s and 0s. There are four rules of binary addition which are:
0+0=0.
0+1=1.
1+0=1.
1+1=10.
Write a program in C++ to add two binary numbers. In the first line, you enter the first binary
number, and in the second line, the second binary number. The third line will print the sum of
two binary numbers.
Input:
101010001
111
Output:
101011000
Question 12:
Problem Statement:
Write a C++ to take a string as input and replaces all the words "Ram" with "Sam" in a string. In
the first line, you enter a string containing the word Ram, Second line will print the string by
replacing the word Ram with Sam.
Input:Ram works in MyAnatomy. Ram is a Ph.D holder
Output:Sam works in MyAnatomy. Sam is a Ph.D holder
Question 13:
Problem Statement:
A factorial is a function that multiplies a number by every number below it. For example,5! =
5*4*3*2*1=120.
Pinku is trying to find the factorial of some natural numbers using a calculator by using formula
n! = n(n-1) (n-2) ….1
But there is a problem with calculator, whenever 2 is pressed alone as a single digit, calculator
takes it as 1. The result he gets is half of what it should be whenever 2 is encountered by the
calculator. Help him write a code using recursion which gives the result as given by the faulty
calculator. Also when non-Natural numbers are put to test, it cannot find the factorial.
Input:5
Output:60
Input: -2
Output: Can't be Calculated
Question 14:
Problem Statement:
An arithmetic progression or arithmetic sequence is a sequence of numbers such that the
difference between the consecutive terms is constant. For instance, the sequence 5, 7, 9, 11, 13,
15.. . is an arithmetic progression with a common difference of 2.
A geometric sequence, is a sequence of non-zero numbers where each term after the first is
found by multiplying the previous one by a fixed, non-zero number called the common ratio.
Example, 2, 4, 8, 16, 32, 64, …, where the common ratio is 2.
Write a C++ program to check whether the sequence of the numbers in a given array is an
“Arithmetic” or “Geometric” sequence.
In the first line, you enter the number of integers in the sequence. In the second line, enter the
numbers in a sequence separated by a space.
If the entered sequence is arithmetic, it will print ARITHMETIC. If the entered sequence is
geometric, it will print GEOMETRIC. Otherwise, NONE.
Input:
5
2 4 6 8 10
Output:
ARITHMETIC
Question 15:
Problem Statement:
The cube of a number is obtained by multiplying the number by itself twice. For example, cube
pf 2 can be calculated as 2 x 2 x 2 = 8. Write a C++ code to calculate the cube of a number using recursion.
Input: 2
Output: 8
Question 16:
Problem Statement:
You are given the address to the first node of a linked list which is presorted, and the data in the
nodes are in ascending order. The linked list might contain duplicate values.
Your job is to delete nodes with duplicate values in the list and return a sorted list with distinct
values in the original list.
Input:
The first line will take integer input N, indicating the number of nodes in the linked list
The second line will have N space separated integers indicating the sorted list with duplicate
values
Output:Distinct values from list in sorted order.
Question 17:
Problem Statement:
Mananjay is trying to count the number of times the disk is inserted in middle stack while
solving the challenge of Tower of Hanoi.
Write a Program to get the number of times a particular number disk is being inserted into the
middle stack after the tower of Hanoi challenge is completed by a user.
Input Explanation:
1st line: number of disks taken for Tower of Hanoi challenge.
2nd line: disk number (from bottom) of which the number of insertion at middle stack is to be
determined.
Output Explanation:
Displays the number of times the required disk is inserted at the middle stack.
Input:
3
2
Output:
1
Question 18:
Problem Statement:
N people have N doors in front of them. The doors are numbered from 1 to N and are stored in
sorted way in a linked list. Initially all doors are shut. Each person goes in order starting from the
first person and shuts the door if it’s open and opens the door if shut. They do so in the following
manner:
When first person goes, he shuts (or open) every door.
When second person goes, he shuts (or open) every second door.
When third person goes, he shuts (or open) every third door.
And this continues on till Nth person goes and he shuts (or open) every Nth door.
After Nth person is done with the work he is given, the objective is to find the doors which are
still open.
Write a Program to delete the door numbers from the linked list which is closed and get the door
numbers which are still open after all this operation.
Input Explanation:
1st line: number of persons (or doors).
Output Explanation:
Displays the space separated sorted linked list node values of door numbers which are open after
the given task.
Input:10
Output:1 4 9
Question 19:
Problem Statement:
Write a Program to get the runs scored by the batsmen in the last five innings. If he played less
than 5 matches, then the score of the match he did not play is to be displayed by minus sign (-).
Store the scores of the batsmen in a queue.
Input Explanation:
1st line: number of matches played by the batsmen.
2nd line: space separated run scored by batsmen in each match he played.
Output Explanation:
Displays the runs scored by batsmen in last five innings.
Input:
3
13 50 10
Output:
- - 13 50 10
Question 20:
Problem Statement:
Sarthak assisted his intern with storing marks scored by the students, roll number wise in a
linked list. Student’s roll number starts with 1 uptoN where N is the total number of students.
Find the marks of student whose roll number comes in between.
If N is even, marks of middle two students is displayed and if N is odd, marks of only the middle
one is displayed.
Input Explanation:
1st line: number of disks students in the class.
2nd line: disk marks scored by each student roll number wise.
Output Explanation:
Displays the marks scored by middle student(/s) .
Input:
5
10 13 15 12 11
Output:
15
Question 21:
Problem Statement:
Prateek is maintaining the goals scored in a game by a particular team using stack. Help him
write a Program to get the minimum number of goals scored by the team in a match.
Input Explanation:
1st line: number of match played by the team.
2nd line: space separated goals scored by the team in each match.
Output Explanation:
Displays the number minimum number of goals scored by the team.
Input:
5
24716
Output:
1
Question 22:
Problem Statement:
Given a queue of integers of even length, write a program to rearrange the elements by
interleaving the first half of the queue with the second half. If length is uneven, return the same
queue as given
Input Explanation:
1st line: number of elements in queue.
2nd line: space separated elements of queue.
Output Explanation:
Displays the interleaved queue.
Input:
4
1234
Output:1 3 2 4
Question 23:
Problem Statement:
Given a queue of integers and an integer k, write a program to reverse the order of first k
elements of the queue, leaving the other elements in the same relative order.
Input Explanation:
1st line: number of elements in queue.
2nd line: space separated elements of the queue.
3rd line: k, number of elements to be reversed from starting
Output Explanation:
Displays the number reversed queue.
Input:
5
12345
3
Output:3 2 1 4 5
Question 24:
Problem Statement:
Students from CSE 3rd year attend their class every day. So, when teachers asked to fill the
feedback form, these students represented the feedback using a binary string (i.e. a string that
contains only characters '0' and '1'.
Now since teacher is not that much into deciphering the coded feedback, he has decided the
following criteria to classify the feedback as Good or Bad:
If the string contains the substring "010" or "101", then the feedback is Good, else it is Bad. Note
that, to be Good it is not necessary to have both of them as substring.
So given some binary strings, you need to decide on output whether according to the
teacher, the strings are Good or Bad.
Input:Each test case contains a string composed of only '0' and '1'.
Output:For every test case, print in a single line Good or Bad as per the teacher’s
method of classification.
Constraints:
1 ≤ T ≤ 100
1 ≤ |S| ≤ 10^5
Input
11111110
Output
Bad
Input
10101010111
Output
Good
Question 25:
Problem Statement:
Create a generic class where it should accommodate two variables of the same type and it should
produce a behavior to display the multiplication on these variables.
Design the driver code in such a way that the user should be able to provide the type from any of
the following: int, double, float types.
Based on the provided type the appropriate output should be displayed by utilizing appropriate
formatters upto 3 decimal places.
Input:
For Each test case the first line will provide the type int, float or double.
Next line will contain two space separated values N, M of the provided type.
Output:
In one line it should display on console the multiplied value with appropriate decimal places.
Constraints:-32,768 <= N,M <= 32,767
Input
float
45.8 56.9
Output
2606.020
Input
int
256 25
Output
6400
Input
double
456.3 432.2
Output
197212.860