// Exercises:

1.    What’s the screen output of running the following program?

2.    Rewrite the recursively defined function algorithm( ) using iteration.

 

//======================================================
import java.util.*;

public class RecurExample0 {

    public static void main(String[] args) {
        int j = 21;
        int k = 5;

        System.out.println("-------OUTPUT------");
        int result = algorithm(j, k);
        System.out.println("result: " + result);
    }

    public static int algorithm(int j, int k) {
        int temp;
        System.out.println(j + "\t" + k);
        if (j < k) {           //base condition
            return j + k;
        } else {
            return algorithm(j / 2, k) + j + k; //recursive call
        }
    }
}