Factorials: An Example Print n in any Base: Another Example

J.E.D.I. Figure 3.1: Factorial Example With iteration, the process terminates when the loop condition fails. In the case of using recursion, the process ends once a particular condition called the base case is satisfied. The base case is simply the smallest instance of the problem. For example, as seen in the recursive definition of factorial, the simplest case is when the input is 1. 1 for this problem is the base case. The use of iteration and recursion can both lead to infinite loops if these are not used correctly. Iteration’s advantage over recursion is good performance. It is faster compared to recursion since passing of parameters to a method causes takes some CPU time. However, recursion encourages good software engineering practice because this technique usually result in shorter code that is easier to understand and it also promotes reusability of a previously implemented solution. Choosing between iteration and recursion is a matter of balancing good performance and good software engineering.

3.2.3 Factorials: An Example

The following code shows how to compute for the nth factorial using the technique of iteration. class FactorialIter { static int factorialint n { int result = 1; for int i = n; i 1; i-- { result = i; } return result; } public static void mainString args[] { int n = Integer.parseIntargs[0]; System.out.printlnfactorialn; } } Introduction to Programming II Page 41 J.E.D.I. Here is the equivalent code that uses the recursion tool instead. class FactorialRecur { static int factorialint n { if n == 1 { The base case return 1; } Recursive definition; Self-invocation return factorialn-1n; } public static void mainString args[] { int n = Integer.parseIntargs[0]; System.out.printlnfactorialn; } } Consider the expected output of the given codes for some integers. Trace what happens in the code for some input. Here are some sample integers with their corresponding output. a Input: 1 1 b Input: 3 6 c Input: 5 120 Introduction to Programming II Page 42 J.E.D.I.

3.2.4 Print n in any Base: Another Example

Now, consider the problem of printing a decimal number in a base specified by the user. Recall that the solution for this is to use repetitive division and to write the remainders in reverse. The process terminates when the dividend is less than the base. Assume that the input decimal number is 10 and we want to convert it to base 8. Here is the solution using pen and paper. From the solution above, 10 is equivalent to 12 base 8. Here is another example. The input decimal is 165 to be converted to base 16. 165 is equivalent to A5 base 16. Note: A=10. This is the iterative solution for this problem. class DecToOthers { public static void mainString args[] { int num = Integer.parseIntargs[0]; int base = Integer.parseIntargs[1]; printBasenum, base; } static void printBaseint num, int base { int rem = 1; String digits = 0123456789abcdef; String result = ; the iterative step while num=0 { rem = numbase; num = numbase; result = result.concatdigits.charAtrem+; } printing the reverse of the result forint i = result.length-1; i = 0; i-- { System.out.printresult.charAti; } } } Introduction to Programming II Page 43 J.E.D.I. Now, this is the recursive equivalent of the solution above. class DecToOthersRecur { static void printBaseint num, int base { String digits = 0123456789abcdef; Recursive step if num = base { printBasenumbase, base; } Base case: num base System.out.printdigits.charAtnumbase; } public static void mainString args[] { int num = Integer.parseIntargs[0]; int base = Integer.parseIntargs[1]; printBasenum, base; } } To gain a better understanding of the code, trace it for a set of input. Here are some sample input. a Num: 10, base: 2 1010 b Num: 100, base: 8 144 c Num: 200, base: 9 242 Introduction to Programming II Page 44 J.E.D.I.

3.3 Abstract Data Types