// Simple program that demonstrates some recursive solutions to problems public class Recursion2 { public static void main(String[] args) { System.out.println(repeatDigits(348)); int[] array = {1, 2, 3}; System.out.println("sum of array: " + sum(array)); } // returns the integer obtained by replacing ever digit of n // with two of that digit. Example: repeatDigits(348) returns 334488. public static int repeatDigits(int n) { if (n < 0) { return -repeatDigits(-n); } else if (n < 10) { return 11 * n; } else { int lastDigit = n % 10; int leadingDigits = n / 10; return repeatDigits(leadingDigits) * 100 + repeatDigits(lastDigit); } } // returns the sum of all the elements in the array. // 0 if the array is empty. public static int sum(int[] array) { return sum(array, 0); } // returns the sum of all the elements in the array starting // at given index. Returns 0 if index is past the end of the array. private static int sum(int[] array, int index) { if (array.length >= index) { return 0; } else { // array[0] + sum (array starting at index 1) // array[1] + sum (array starting at index 2) == 3 // array[2] + sum(array starting at index 3) return array[index] + sum(array, index + 1); } } }