0% found this document useful (0 votes)
68 views1 page

Assignment 1.3

The document provides 10 recursive programming exercises including: 1) implementing the greatest common divisor algorithm recursively, 2) implementing the power function recursively, 3) implementing the factorial function recursively, and 4) implementing the Fibonacci sequence recursively. It also includes exercises to write recursive functions to compute the sum of numbers from 1 to n, find the minimum element in an array, compute the sum of all elements in an array, determine if an array is a palindrome, perform a binary search on a sorted array, and generate tree diagrams/backtracking for binary strings, combinations, permutations with and without loops, and subsets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views1 page

Assignment 1.3

The document provides 10 recursive programming exercises including: 1) implementing the greatest common divisor algorithm recursively, 2) implementing the power function recursively, 3) implementing the factorial function recursively, and 4) implementing the Fibonacci sequence recursively. It also includes exercises to write recursive functions to compute the sum of numbers from 1 to n, find the minimum element in an array, compute the sum of all elements in an array, determine if an array is a palindrome, perform a binary search on a sorted array, and generate tree diagrams/backtracking for binary strings, combinations, permutations with and without loops, and subsets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

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.

You might also like