Exercises
Recursion #1

Use the following function puzzle(..) to answer problems 1 - 3.

	int puzzle(int base, int limit)
	{	//base and limit are nonnegative numbers
		
		if ( base > limit )
			return -1;
		else if ( base == limit )
			return 1;
		else
			return base * puzzle(base + 1, limit);
	}
  1. Identify the base case(s) of function puzzle(..)

  2. Identify the recursive case(s) of function puzzle(..)
  3. Show what would be displayed by the following calls.
    1. System.out.print(puzzle(14,10));
    2. System.out.print(puzzle(4,7));
    3. System.out.print(puzzle(0,0));
    In problems 4 - 6, show the output that will be displayed by the call show(123);
  4.  
  5.  
  6. void show ( int n)
    {
    	if (n)
    		show (n/10);
    	System.out.print((n%10));
    }
    	void show ( int n)
    {
    	System.out.print((n%10));
    	if (n)
    		show (n/10);
    }
    	void show ( int n)
    {
    	System.out.print((n%10));
    	if (n)
    		show (n/10);
    	System.out.print((n%10));
    }
  7. Complete the Java code to recursively evaluate the sum: sum = 1 + 1/2 + 1/3 +...+1/n, n 1.

    double sum(int n)		// n>=1
    {
    	if (___________) 
    		return _____________;
    
    	return ______ + sum(______);
    }		


Continue to:  Unit 3  / Prev  / Next