Lab Exercise 04 Object Oriented Programming Lab: National University of Computer and Emerging Sciences
Lab Exercise 04 Object Oriented Programming Lab: National University of Computer and Emerging Sciences
Lab Exercise 04
For
Object Oriented Programming Lab
Page 1|8
National University of Computer & Emerging Sciences – FAST (CFD)
Department of Computer Science: Object Oriented Programming - Lab
Instructions:
1. Make a word document with the naming convention “SECTION_ LAB#_ROLLNO” and
put all your source code and snapshots of its output in it. Make sure your word file is
formatted properly.
2. Plagiarism is strictly prohibited.
3. Do not discuss solutions with one another.
Useful links
Question#1
Write a function to recursively see if a number is prime or not, the function should return true
when the base case has met, false otherwise.
Question#2
Write a recursive code to find the factorial of a number. Then take two inputs a and b from user,
and print the factorials of all the numbers in that range.
For example,
Input a: 2
Input b: 5
Output:-
Factorial of 2: 2
Factorial of 3: 6
Factorial of 4: 24
Factorial of 5: 120
Question#3 (Recursion)
Page 2|8
National University of Computer & Emerging Sciences – FAST (CFD)
Department of Computer Science: Object Oriented Programming - Lab
Question#4 (Recursion)
Write a function to recursively test if a string is palindrome or not, the function should return true
when the base case has met, false otherwise. For example, string ‘aaa’ is a palindrome and ‘abab’
is not a palindrome. Remember you need to provide a recursive solution.
Question#5 (Recursion)
Write a recursive function to reverse a string. Write a recursive function to reverse the words in a
string, i.e., “cat is running” becomes “running is cat”.
Question#6 (Recursion)
Write recursive code that computes all permutations of a string. For Example, if ourstring is abc,
the permutations are: abc, bac, bca, acb, cab, cba. The following algorithm works this way:
3. Insert the 'a' into "bc", so that we have "abc", "bac", and "bca". In the same way, put 'a' into
"cb", then we get "acb", "cab", and "cba".
Question#7 (Recursion)
Page 3|8
National University of Computer & Emerging Sciences – FAST (CFD)
Department of Computer Science: Object Oriented Programming - Lab
Question#8 (Recursion)
Question#9 (Recursion)
The Tower of Hanoi is a mathematical puzzle. It consists of three poles and a number of disks
of different sizes which can slide onto any poles. The puzzle starts with the disk in a neat stack
in ascending order of size in one pole, the smallest at the top thus making a conical shape. The
objective of the puzzle is to move all the disks from one pole (say ‘source pole’) to another
pole (say ‘destination pole’) with the help of the third pole (say auxiliary pole).
The puzzle has the following two rules:
1. You can’t place a larger disk onto a smaller disk
2. Only one disk can be moved at a time.
Example:
Input: 3
Output:
Move disk 1 from rod A to rod B.
Move disk 2 from rod A to rod C.
Move disk 3 from rod A to rod D.
Move disk 2 from rod C to rod D.
Move disk 1 from rod B to rod D.
Input: 5
Output:
Move disk 1 from rod A to rod C.
Move disk 2 from rod A to rod D.
Move disk 3 from rod A to rod B.
Move disk 2 from rod D to rod B.
Move disk 1 from rod C to rod B.
Move disk 4 from rod A to rod C.
Page 4|8
National University of Computer & Emerging Sciences – FAST (CFD)
Department of Computer Science: Object Oriented Programming - Lab
Figure 7
Figure 8
Page 5|8
National University of Computer & Emerging Sciences – FAST (CFD)
Department of Computer Science: Object Oriented Programming - Lab
#10 (Recursion)
Given the following three values, the task is to find the total number of maximum chocolates
you can eat.
1. money: Money you have to buy chocolates.
2. price: Price of a chocolate
3. wrap: Number of wrappers to be returned for getting one extra chocolate.
It may be assumed that all given values are positive integers and greater than 1.
Examples:
Input: money = 16, price = 2, wrap = 2
Output: 15
Price of a chocolate is 2. You can buy 8 chocolates from
amount 16. You can return 8 wrappers back and get 4 more.
chocolates. Then you can return 4 wrappers and get 2 more
chocolates. Finally, you can return 2 wrappers to get 1.
more chocolate.
Question#11 (Recursion)
Suppose you have 8 chess queens......and a chess board.
Can the queens be placed on the board so that no two queens are attacking each other.
Page 6|8
National University of Computer & Emerging Sciences – FAST (CFD)
Department of Computer Science: Object Oriented Programming - Lab
Two queens are not allowed in the same row, or in the same column...
Two queens are not allowed in the same row, or in the same column, or along the same
diagonal.
The number of queens, and the size of the board can vary.
Page 7|8
National University of Computer & Emerging Sciences – FAST (CFD)
Department of Computer Science: Object Oriented Programming - Lab
return true;
}
Algorithm nQueens(k, n) :
// Using backtracking, this procedure prints all
// possible placements of n queens on an n×n
// chessboard so that they are nonattacking.
{
for i:= 1 to n do
{
if Place(k, i) then
{
x[k] = i;
if (k == n)
write (x[1:n]);
else
NQueens(k+1, n);
}
}
}
Input: 4 Queens on a 4x4 Board
Output:
Page 8|8