Tutorial Programming 2 & OOP-Copy
Tutorial Programming 2 & OOP-Copy
4. Let’s consider the following algorithm to calculate the product of two numbers:
𝑎 ∗ (𝑏 − 1) + 𝑎 𝑖𝑓 𝑏 𝑖𝑠 𝑜𝑑𝑑
𝑎∗𝑏 ={ 𝑏
(2 ∗ 𝑎) ∗ ( ) 𝑖𝑓𝑏 𝑖𝑠 𝑒𝑣𝑒𝑛 𝑎𝑛𝑑 𝑏 ≠ 0
2
a) Write a recursive function which calculate the product of two numbers according to the
above algorithm.
b) Write an iterative function which calculate the product of two numbers according to the
above algorithm.
c) For each function defined above, write a main program using it to calculate the product of
two integers entered by the user.
d) Write an iterative program to calculate the product of two numbers using the above
algorithm and print the result as shown in the following example:
5. Write a function which generate 50 integers between 1 and 100 randomly and do the
follow. It calculates the factorial of each number divisible by 5 of and the inverse of each
number divisible by 6. You must use two functions facto and inverse to calculate the
factorial and the inverse of any number. Static variables should be used in those functions
to count the number of time each of them has been called.
6. Build a program that uses a single-dimension array to store 10 numbers input by a user.
After inputting the numbers, the user should see a menu with two options to sort and print
the 10 numbers in ascending or descending order.
7. Write a calculator class which will accept two values as arguments, has 4 methods (add,
subtract, multiply and divide). Test the above class with the values 10 and 5.