3-Recursion
Practical exercises
Write Java programs to perfom the following tasks:
1. Implement the Greatest Common Divisor algorithm as recursive method GCD. Use recursion.
Do NOT use a loop.
2. Implement the power function recursively
3. Implement the factorial function recursively as fact
4. Implement Fibonacci recursively as f
Remove the tail of recursion
5. Write a recursive function that computes the sum of all numbers from 1 to n, where n is given
as parameter.
//return the sum 1+ 2+ 3+ ...+ n
int sum(int n)
6. Write a function that finds and returns the minimum element in an array, where the array and
its size are given as parameters.
//return the minimum element in a[]
int findmin(int a[], int n)
7. Write a recursive function that computes and returns the sum of all elements in an array,
where the array and its size are given as parameters.
//return the sum of all elements in a[]
int findsum(int a[], int n)
8. Write a recursive function that determines whether an array is a palindrome, where the
array and its size are given as parameters.
//returns 1 if a[] is a palindrome, 0 otherwise. The string a is palindrome
if it is the same as its reverse.
int ispalindrome(char a[], int n)
9. BINARY SEARCH: Write a recursive function that searches
for a target in a sorted array using binay search, where the array,
its size and the target are given as parameters.
10. Write the programs for enumerate (tree diagram/backtracking): binary
string, combinator, permutation loop/non-loop, subsets.