Sample Recursion Problems, P. Conrad

  1. Write a function with the following prototype that returns the sum of the digits of an integer.
    int sumOfDigits(int x);
    

    If x is 234, the function should return 2 + 3 + 4, that is, 9.

    If x is 12, the function should return 1 + 2, which is 3.

    If x is 39, the function should return 12.

    If x is negative, ignore the minus sign. For example, -12 and 12 both return 3.

    Use recursion.

    Hints:

  2. Write a recursive function (use no while loops or for loops) that prints all the elements of an array of integers, one per line. The parameters to the function should be int a[], and int size.

    Hint :

  3. Same problem as the last one, but print out the elements in reverse order.

    Hint :

  4. Find the sum of the integers from 1 through n. Use recursion.
  5. Find the product of the integers from 1 through n (this is called the factorial function). If n is zero, return 1. Use recursion.
  6. Count the number of zeros in an array of integers. Use recursion.
  7. Find the minimum element in an array of integers. Use recursion.