SIBT COMP 125

Week 9 Practical Exercises

1. Based on the code given in lectures, write a recursive program which reads a positive integer n from input and prints n!, that is, n.(n-1).(n-2)...2.1.

Hint: 1! = 1, 0! = 1, and n! = n((n-1)!).

Test your program by calculating the value of 10!

2. Write a recursive function

max(A : ARRAY[integer]; n : integer ) : integer

which returns the maximum element in the array A(1..n), where n > 0.

3. Euclid's algorithm for finding the greatest common divisor (GCD) of two nonnegative integers n and m, where n < m, works as follows:

If n = 0, the GCD is m. If not, the GCD is equal to the GCD of m \\ n and n.

Implement this, firstly, using an iterative solution, then try it using a recursive function.