Professional Documents
Culture Documents
Recursion Assignment 1.3
Recursion Assignment 1.3
Practical exercises
Write Java programs to perfom the following tasks. Note: You should use recursion, DO NOT
use a loop.
1. Implement the Greatest Common Divisor algorithm as recursive method GCD.
5. Write a recursive method searching the result of the remainder of division (%) of a and b by
using the subtraction only.
6. Implement a recursive method finding the result of division (/) of a and b by using the
subtraction only.
7. Implement a recursive method that computes 2n by using the sumation only.
8. Implement a recursive method that tranforms a number from decimal to binary, octal or
hexadecimal.
9. Implement a recursive method that reverses a number. For example: n= 1234 4321
Remove the tail of recursion
10. Write a recursive method that computes and returns the sum of all numbers from 1 to n (sum
= 1 + 2 + 3 + ... + n), with n is given as parameter.
int sum(int n)
6. Write a recursive method that finds and returns the minimum element in an array of integer,
with the array and its size are given as parameters.
int findMin(int a[], int n)
7. Write a recursive method that computes and returns the sum of all elements in an array of
integer, with the array and its size are given as parameters.
int findSum(int a[], int n)
8. Write a recursive method that determines whether an array of characters is a palindrome or
not?.
Note:
- An array of characters and its size are given as parameters.
- returns true if an array of characters is a palindrome, otherwise return false.
- int isPalindrome(char[] a, int n)
9. BINARY SEARCH: Write a recursive method that searches for a target in a sorted array
using binay search algorithm, with the array and its size and the target are given as parameters.
10. Write a recursive method solving SaiGon tower that tranforms n disks from column a to
column d, with b & c are the imtermediate columns.
11. Write the programs for enumerate (tree diagram/backtracking): binary string, combinator,
permutation loop/non-loop, subsets.